Infinite windows message loop

与世无争的帅哥 提交于 2019-12-02 21:17:26

问题


I have this message loop in my program:

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();
    }
}

This loop never ends. It never displays the message box even though main window disappears. Here is the WndProc code:

switch (msg) {

    case WM_CLOSE :
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY :
        PostQuitMessage(0);
        break;

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

return 0;

Could someone please help me? I am literally pulling my hairs out.


回答1:


You're calling PeekMessage(&msg, window, ...). If window isn't NULL, you'll never get WM_QUIT, because WM_QUIT is not associated with a window.

Instead, just call PeekMessage/GetMessage with a NULL HWND. DispatchMessage will send it to the right WndProc as necessary. (In general, making GetMessage/PeekMessage filter by HWND is a bad idea.)



来源:https://stackoverflow.com/questions/10658813/infinite-windows-message-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!