0

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
  • Have you tried tracing messages with Spy++ or a similar tool? Commented May 18, 2012 at 19:52

1 Answer 1

9

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.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.