Thread: PeekMessage?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    43

    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.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    NULL should typically be used for the second argument of PeekMessage or GetMessage. This allows all messages belonging to the thread to be processed.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What on earth is the "NULL message"? There's only WM_QUIT, which is a thread message and thus has a NULL window handle.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    Ah, passing NULL fixed it.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. windowproc vs. peekmessage
    By CuLtOfGoAt in forum Windows Programming
    Replies: 4
    Last Post: 06-04-2005, 02:32 PM
  2. PeekMessage Windows XP
    By wgdb198 in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2005, 07:46 AM
  3. PeekMessage function
    By Micko in forum Windows Programming
    Replies: 9
    Last Post: 09-14-2004, 03:06 PM
  4. 100%cpu usage with PeekMessage
    By glUser3f in forum Windows Programming
    Replies: 6
    Last Post: 08-19-2003, 06:56 AM
  5. PeekMessage
    By xds4lx in forum Windows Programming
    Replies: 13
    Last Post: 08-26-2002, 10:51 PM