windows GetMessage function

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
optLog
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Wed Oct 06, 2010 3:40 am

windows GetMessage function

Post by optLog »

I just started learning about the windows message loop and how to use the functions GetMessage and PeekMessage. While I was making a test application using GetMessage for the message loop I had trouble getting the program to close.

When I use the following message loop / Window Procedure, GetMessage won't get the WM_QUIT message until after I move the window who's handle I passed to GetMessage too and then pressed the close button.

Code: Select all

//Message Loop
while(GetMessage(&msg, (HWND)window, 0, 0) > 0)
{
     TranslateMessage(&msg);
     DispatchMessage(&msg);
}
//Window Procedure
if(msg.message == WM_CLOSE)
{
    PostQuitMessage(0);
    return 0;
}
else return DefWindowProc(window, msg, wParam, lParam);
However, when I didn't pass a windows handle to GetMessage the program ended just like I wanted it to. This confuses me because as far as I understand it the reason that GetMessage returns 0 (meaning it got the WM_QUIT message) is because its getting all the messages for the thread rather than just that of the window. This leads me to think that PostQuitMessage doesn't post the quit message to any of the windows message queues. But then why does GetMessage return 0 when I use it to get just the messages for a single window (at least I think I'm just getting the messages for a single window)? Can anyone explain to me what's happening?
Post Reply