WinAPI 窗口立即关闭
WinAPI Window Closes Instantly
我一直在尝试使用WINAPI试图学习它,但是我创建的窗口立即关闭。如您所见,当按下W键或按下左按钮时,它将关闭程序,但是在没有按下按钮的情况下运行它时,它仍然会关闭。
#include <windows.h>
#include <windowsx.h>
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// the handle for the window, filled by a function
HWND hWnd;
// this struct holds information for the window class
WNDCLASSEX wc;
// clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));
// fill in the struct with the needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass1";
// register the window class
RegisterClassEx(&wc);
// create the window and use the result as the handle
hWnd = CreateWindowEx(NULL,
L"WindowClass1", // name of the window class
L"Game", // title of the window
WS_OVERLAPPEDWINDOW, // window style
1, // x-position of the window
1, // y-position of the window
1800, // width of the window
1000, // height of the window
NULL, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL
// display the window on the screen
ShowWindow(hWnd, nCmdShow);
// enter the main loop:
// this struct holds Windows event messages
MSG msg;
// wait for the next message in the queue, store the result in 'msg'
while (GetMessage(&msg, NULL, 0, 0))
{
// translate keystroke messages into the right format
TranslateMessage(&msg);
// send the message to the WindowProc function
DispatchMessage(&msg);
}
// return this part of the WM_QUIT message to Windows
return msg.wParam;
}
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// sort through and find what code to run for the message given
switch (message)
{
// this message is read when the window is closed
case WM_MOUSEMOVE:
{
// Retrieve mouse screen position
int x = (short)LOWORD(lParam);
int y = (short)HIWORD(lParam);
// Check to see if the left button is held down:
bool leftButtonDown = wParam & MK_LBUTTON;
// Check if right button down:
bool rightButtonDown = wParam & MK_RBUTTON;
if (leftButtonDown == true)
{
//left click
//example lets close the program when press w
PostQuitMessage(0);
return 0;
}
}
case WM_KEYDOWN:
{
switch (wParam)
{
case 'W':
//w pressed
//example lets close the program when press w
PostQuitMessage(0);
return 0;
}
}
case WM_DESTROY:
{
// close the application entirely
PostQuitMessage(0);
return 0;
}
default:
break;
}
// Handle any messages the switch statement didn't
return DefWindowProc(hWnd, message, wParam, lParam);
}
你的开关中缺少一些break
语句,所以例如,如果你得到WM_MOUSEMOVE
消息和leftButtonDown != true
,执行将下降到WM_KEYDOWN
,等等。
最终你会得到case WM_DESTROY:
,这将Post
你一个可爱的QuitMessage
。
顺便说一句,通过在调试器中逐条单步执行,这很容易发现。
您的switch
语句中没有break
。
你最终执行
PostQuitMessage(0);
你可以做这样的事情:
case WM_FOO:
{
if ( bar ) {
return 0;
}
break;
}
不要通过WM_MOUSEMOVE
消息检测点击,请改用WM_MOUSEDOWN
。
问题是你的代码可能是通过你点击某些东西启动的,所以当你的窗口收到它的第一条WM_MOUSEMOVE
消息时,按钮仍然被实际按下。代码运行速度比手指快得多。
相关文章:
- WinAPI 检查窗口是否具有常规标题栏
- WinAPI 窗口在使用 PrintWindow 截屏后不再更新
- 如何停止在所有桌面上显示C / C++ WINAPI窗口?
- 隐藏由 WinAPI 调用创建的窗口
- 如何使用WinAPI每帧只重新绘制一次子窗口
- C++ winAPI 无法处理子窗口事件
- 使用winapi使用按钮更改窗口的背景颜色
- 禁用"Script Error"弹出窗口 IWebBrowser2 c++ WinApi
- Winapi Window窗口异常破坏多个作品
- 如何在C Winapi中获取活动文件Explorer窗口的路径
- WinAPI - 确定窗口是否具有最大化/还原功能
- WinAPI.检查窗口大小调整是否已完成
- 窗口的C Winapi屏幕截图
- 如何获取具有可见窗口的任何进程的名称 - WinAPI?
- 如何使用Winapi设置最小窗口尺寸
- 从 WinAPI 窗口创建控制台窗口
- 使用 winAPI 设置窗口的高性能电源计划C++
- 如果 winapi 窗口最小化,如何恢复它?
- C++ & WinApi - 在创建的窗口中将文本从键盘输出到屏幕
- 简单窗口WinAPI面向对象