【问题标题】:Infinite windows message loop无限窗口消息循环
【发布时间】:2012-05-26 09:25:54
【问题描述】:

我的程序中有这个消息循环:

while (true) {
    if (PeekMessage(&msg, window, 0, 0, PM_REMOVE)) {
        if (msg.message == WM_QUIT) {
            MessageBox(NULL, L"Quit", L"", 0);
            break;
        }
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    } else {
        Render();
    }
}

这个循环永远不会结束。即使主窗口消失,它也不会显示消息框。 这是 WndProc 代码:

switch (msg) {

    case WM_CLOSE :
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY :
        PostQuitMessage(0);
        break;

    default :
        return DefWindowProc(hwnd, msg, wParam, lParam);
        break;
}

return 0;

有人可以帮我吗?我真的把我的头发拉出来了。

【问题讨论】:

  • 您是否尝试过使用Spy++ 或类似工具跟踪邮件?

标签: c++ c windows winapi message


【解决方案1】:

你打电话给PeekMessage(&msg, window, ...)。如果window 不是NULL,您将永远不会得到WM_QUIT,因为WM_QUIT 与窗口无关。

相反,只需使用NULL HWND 调用PeekMessage/GetMessageDispatchMessage 将根据需要将其发送到右侧WndProc。 (一般情况下,making GetMessage/PeekMessage filter by HWND is a bad idea.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 2018-11-29
    相关资源
    最近更新 更多