Search code examples
cwinapiwindowmessage-loop

Win32 Message Loops: Quitting after window closes with GetMessage(&msg, NULL, 0, 0)?


If I have the following code below, how do I detect when the window has been closed, so I can quit? r never seems to get the value -1 0, and I need to process messages for the entire thread, not just the current window.

HWND hWnd = CreateWindowExW(0, L"Edit", L"My Window", WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 300, 200, NULL, NULL, NULL, NULL);
ShowWindow(hWnd, SW_SHOWDEFAULT);
MSG msg;
BOOL r;
while ((r = GetMessageW(&msg, NULL, 0, 0)) != 0)
{
    if (r == -1) { break; }
    TranslateMessage(&msg);
    DispatchMessageW(&msg);
}

Solution

  • Waiting for r = -1 is not the way you detect that your window has closed. A return value of -1 is not a normal condition: it indicates that an error has occurred in the message loop.

    From the documentation:

    Return Value

    Type: BOOL

    If the function retrieves a message other than WM_QUIT, the return value is nonzero.

    If the function retrieves the WM_QUIT message, the return value is zero.

    If there is an error, the return value is -1. For example, the function fails if hWnd is an invalid window handle or lpMsg is an invalid pointer. To get extended error information, call GetLastError.

    When GetMessage retrieves a WM_QUIT message from the queue, it will return a value of 0, and you should end the loop.

    If you just want to know when the window has closed, you probably want to handle either the WM_CLOSE or WM_DESTROY messages. For a discussion of these messages, see the answers to this question: What is the difference between WM_QUIT, WM_CLOSE, and WM_DESTROY in a windows program?