【发布时间】:2021-01-03 17:10:25
【问题描述】:
拥有如此超级简单的Win app:
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow) {
...
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow)) {
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GL1WIN));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0)) {
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_COMMAND: {
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId) {
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_CLOSE:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
当 WM_CLOSE 案例被禁用(注释掉)时,一切正常 - 当我单击 X 按钮时,窗口关闭,但当 WM_CLOSE 案例像上面的代码一样启用时,应用程序似乎冻结了 -单击X 按钮时没有反应,我必须从task manager 中杀死它。
【问题讨论】:
-
当消息循环收到
WM_CLOSE时,您认为您的代码会做什么?您直接转到return 0;声明。或者,也许我应该问:你想要你的代码做什么?如果您不处理该消息,则应返回一个非零值。 -
WM_CLOSE 情况下的代码是什么无关紧要。可能只有
OutputDebugString(L"WM_CLOSE ");指令。我的问题是,正如我已经写过的那样,只有 WM_CLOSE 案例代码中的礼物才会使应用程序冻结。
标签: c++ windows visual-studio