-
PeekMessage?
So I started DirectX programming, and I've learned to use PeekMessage instead of GetMessage, but for some reason the program keeps running in the background when I close it. I have searched the forums, but I couldn't find a working solution. Here's my code:
Message Loop:
Code:
ZeroMemory ( &Msg, sizeof ( MSG ) );
while ( Msg.message != WM_QUIT )
{
if ( PeekMessage ( &Msg, hWnd, 0, 0, PM_REMOVE ) )
{
TranslateMessage ( &Msg );
DispatchMessage ( &Msg );
}
else
{
// Rendering, etc.
}
}
Window Procedure:
Code:
LRESULT CALLBACK WndProc ( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam )
{
switch ( Msg )
{
case WM_CLOSE:
DestroyWindow ( hWnd );
break;
case WM_DESTROY:
PostQuitMessage ( WM_QUIT );
break;
default:
return DefWindowProc ( hWnd, Msg, wParam, lParam );
}
return 0;
}
Thanks.
-
Why do you use PeekMessage? It's not meant to be used this way. WM_DESTROY posts NULL message to the message queue and GetMessage returns 0 (false) then and the loop breaks. PeekMessage doesn't care about NULL message. You would need to handle NULL messages separately then.
-
NULL should typically be used for the second argument of PeekMessage or GetMessage. This allows all messages belonging to the thread to be processed.
-
What on earth is the "NULL message"? There's only WM_QUIT, which is a thread message and thus has a NULL window handle.
-
Ah, passing NULL fixed it.
Thanks. :)