在我的 C++ 应用程序的 GUI 对象中,我在主窗口过程中有以下内容:
case WM_SIZE:
{
OutputDebugString(L"WM_SIZE received.\n");
RECT rect = {0};
GetWindowRect(hwnd, &rect);
if (!PostMessage(0, GUI_MSG_SIZECHANGED, w, MAKELONG(rect.bottom - rect.top, rect.right - rect.left))) {
OutputDebugString(L"PostMessage failed.\n"); // <--- never called
}
}
return 0; // break;
GUI 对象还具有以下 getMessage() 方法:
int GUI::getMessage(MSG & msg) {
BOOL result = 0;
while ((result = GetMessage(&msg, 0, 0, 0)) > 0) {
if (msg.message > (GUI_MSG_BASE-1) && msg.message < (GUI_MSG_LAST+1)) {
OutputDebugString(L"GUI message received.\n");
break;
}
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return result;
}
应用程序对象通过以下方式调用此方法:
while ((result = _gui.getMessage(msg)) > 0) {
switch (msg.message) {
// TODO: Add gui message handlers
case GUI_MSG_SIZECHANGED:
OutputDebugString(L"GUI_MSG_SIZECHANGED received.\n");
_cfg.setWndWidth(HIWORD(msg.lParam));
_cfg.setWndHeight(LOWORD(msg.lParam));
if (msg.wParam == SIZE_MAXIMIZED)
_cfg.setWndShow(SW_MAXIMIZE);
else if (msg.wParam == SIZE_MINIMIZED)
_cfg.setWndShow(SW_MINIMIZE);
else if (msg.wParam == SIZE_RESTORED)
_cfg.setWndShow(SW_SHOWNORMAL);
break;
}
}
应用程序对象对窗口大小感兴趣,因为它将这些信息存储在配置文件中。
当我在 Visual Studio 的调试器中运行它时,调整窗口大小后输出窗口如下所示:
WM_SIZE received.
GUI message received.
GUI_MSG_SIZECHANGED received.
WM_SIZE received.
WM_SIZE received.
WM_SIZE received.
WM_SIZE received.
...etc...
PostMessage() 函数永远不会失败,但似乎只在第一次处理 WM_SIZE 时发送 GUI_MSG_SIZECHANGED (#defined as WM_APP + 0x000d),也就是在处理 WM_CREATE 之后。
我不知道是什么原因造成的。我尝试使用 SendMessage 和 PostThreadMessage 但结果是一样的。还阅读了 MSDN 的消息处理文档,但找不到我的代码有什么问题。
有人可以帮忙吗?