Karl
Karl

Reputation: 23

WM_CLOSE in modal message loop

I am using a special message loop for a custom dialog box. When the dialog box is open and the window is closed, I would like to reach the second if below, if(msg.message == WM_CLOSE).

for(;;)
{
    if(PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
    {
        if(msg.message == WM_CLOSE)
        {
            GetMessage(&msg, 0, 0, 0);
            break;
        }

        else
        {
            if(GetMessage(&msg, 0, 0, 0))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }

            if(msg.message == msgEnd)
                break;
        }
    }

The problem is the code in this if is never reached. I tried replacing WM_CLOSE with another message to see if the loop was the problem, but the other message worked fine. What's wrong?

Upvotes: 2

Views: 1092

Answers (1)

hamstergene
hamstergene

Reputation: 24429

It seems that WM_CLOSE is sent, and the other message is posted.

GetMessage and PeekMessage only operate on posted messages (those posted with PostMessage). If a message is not posted but sent via SendMessage, it's handled immediately inside PeekMessage or GetMessage, so you can not get MSG struct for it.

Upvotes: 4

Related Questions