WM_DESTROY message confusion

Viewed 128

Having such a simple Win application:

bool continueRunning = true;
...
int APIENTRY wWinMain(...) {
        while (continueRunning) {

            PeekMessage(&msg, 0, 0, 0, PM_REMOVE);

            TranslateMessage(&msg);
            DispatchMessage(&msg);

            if (WM_DESTROY == msg.message) {
                OutputDebugString(L"--- WM_DESTROY (WinMain) ---\n");
            }   
        }
}

...

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {

    switch (message) {      

        case WM_DESTROY:
            OutputDebugString(L"--- WM_DESTROY (WndProc) ---\n");
            continueRunning = false;
            break;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

I got --- WM_DESTROY (WndProc) --- message but not --- WM_DESTROY (WinMain) ---.

How does the message WM_DESTROY is passed to the app since the PeekMessage doesn't get it?

1 Answers

PeekMessage (or GetMessage) dispatches sent messages (from other threads) on its own without returning them. Only posted messages are returned by it. If a message is sent by the same thread, it does not even go via PeekMessages, it is effectively a direct window procedure call. WM_DESTROY is sent.

By the way, avoid not passing system messages to DefWindowProc, including WM_DESTROY.

Related