c++windowswinapimessage-looppeekmessage

How does Win32 API PeekMessage function work


I was trying to program with Win32 API, but I came across an unusual behaviour that I wasn't expecting which left me kinda confused. In my while loop:

while (IsWindow(hwnd)) {
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
        TranslateMessage(&msg); DispatchMessage(&msg);
    }

    OutputDebugStringA("loop continues\n");
}

and only when I am not moving the window everything works as expected, but when I try to move the window by move dragging the title bar the loop doesn't continue for some reason. I thought that is happening because I am trying to it PeekMessage with while loop but the same behaviour shows even with an if statement.

I would like to know what this happens and how can I make it so the rest of the loop gets executed. I don't know how the message queuing and dispatching works so I need some help.


Solution

  • There are several situations where the system spins up a modal message loop for you. This happens, for example, when entering a menu or displaying a modal dialog box.

    In this case, it is the moving or sizing modal loop (see the WM_ENTERSIZEMOVE message). This is how the system is designed to work. Your message loop gets "suspended" (by a nested modal loop) until the moving or sizing operation ends.

    You'd see the same behavior with a standard GetMessage()-based message loop. This isn't a peculiarity of PeekMessage().