像往常一样,我遇到了一个非常奇怪的问题。
事实上,我不太确定问题到底是什么,但我确定症状是什么。 应用程序正在接收消息、处理消息并调用 API,但没有任何效果,除非在某些看似异常的情况下。
我正在尝试捕捉按键,即转义键。当应用程序收到它时,它会调用 PostQuitMessage()
并稍后完成处理(清理)。
问题是,当调用 PostQuitMessage()
时,什么也没有发生。
窗口仍然坐在那里,我假设 API 失败(但它返回 void,所以我无法判断),因为我在 Spy++ 上没有看到任何引用 WM_QUIT
、WM_CLOSE
或类似的内容。
导致窗口关闭的操作包括单击关闭按钮 [x] 或从非客户区或标题栏拖动窗口,然后按 Esc 键。只需单击窗口,“alt-tabbing?”到窗口,尽管正在处理消息,但我能想到的所有其他内容都不允许窗口响应。
我将在下面发布相关代码。如果有人有任何要求、建议或解决方案,欢迎提出!感谢您抽出宝贵的时间,祝您有美好的一天。
这是窗口过程;它的地址以与 this 中描述的方法类似的方式存储在 GWLP_USERDATA
中。文章。我之前在其他应用程序中使用过这个,从未遇到过这个问题,收到的句柄是有效的 - 该功能不起作用!?
LONG_PTR MainWindow::HandleMessage(UINT Message,
WPARAM WParam, LPARAM LParam) {
switch(CurrentState) {
case Introduction:
return HandleIntroMessage(Message, WParam, LParam);
default:
return DefWindowProc(Window(), Message, WParam, LParam);
}
}
LONG_PTR MainWindow::HandleIntroMessage(UINT Message,
WPARAM WParam, LPARAM LParam) {
switch(Message) {
case WM_KEYDOWN:
switch (WParam) {
case VK_ESCAPE:
PostQuitMessage(0);
return false;
}
case WM_DESTROY:
PostQuitMessage(0);
default:
return DefWindowProc(Window(), Message, WParam, LParam);
}
}
以及wWinMain()
主体的一部分。
std::unique_ptr<ApplicationMutex> EnsureOneInstance(new ApplicationMutex);
/*
* If the instance is not first, return with the return value of the API
* SetForegroundWindow after trying to find the window.
*/
if(!EnsureOneInstance->IsInstanceFirst(L"SDV") ) {
(SetForegroundWindow(FindWindow(nullptr, L"SDV") ) );
return 1;
}
/*
* Create and show our main window; initialize the frame.
*/
std::unique_ptr<MainWindow> MainWin(new MainWindow() );
MainWin->SwitchState(Introduction);
MainWin->CreateWindowWithUserFormat();
ShowWindow(MainWin->Window(), SW_SHOWNORMAL);
SetActiveWindow(MainWin->Window() );
SetFocus(MainWin->Window() );
assert(MainWin->Window() != nullptr);
std::unique_ptr<ApplicationEngine> SDV(new ApplicationEngine(MainWin->Window(),
ThisInstance, CmdLine, CmdShow) );
SDV->Initialize();
MSG Message;
int Res = 1;
while(Res = GetMessage(&Message, MainWin->Window(), 0, 0) > 0) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
if(Res == -1) {
return GetLastError();
}
再次感谢您的宝贵时间。
最佳答案
在 GetMessage
调用中,您仅处理 MainWin->Window()
的消息,但 PostQuitMessage
发布的线程消息不是不绑定(bind)到任何特定窗口。因此,您的 GetMessage
将永远不会检索到它。您应该为第二个参数传递 NULL 而不是窗口句柄。
(此外,由于运算符优先级,您会遇到逻辑错误 - Res
只会是 1 或 0,而不是 -1,尽管这不会导致您的问题)。
关于winapi - 窗口不响应消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17459460/