Ruben Trancoso
Ruben Trancoso

Reputation: 1440

Windows message loop

Theres some reason for this code not reach the first else? I got it exactly the same from vairous sources. Than I did my own encapsulation. Everything goes fine. Window is created, messages are treated, events are generated to keyborad input in the client area, the gl canvas works fine (when I force it to draw).

The only problem is that message loop never leaves the first if. :/ I'm really stuck.

while (!done)                                       
{
    if (::PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
    {
        if (msg.message == WM_QUIT)                 
        {
            done = TRUE;                            
        }
        else                                        
        {
            ::TranslateMessage (&msg);              
            ::DispatchMessage (&msg);               
        }
    }
    else                                        
    {
        // Code is never reaching this!
        draw ();
        ::SwapBuffers(hDC);
        idle ();
    }
}
return msg.wParam;

Upvotes: 1

Views: 3453

Answers (4)

morechilli
morechilli

Reputation: 9817

In your case the message queue must never be empty - why? Well it depends on what the rest of your program is doing. Some possibilities:

  1. Your code is posting new messages to the queue in a manner such that the queue doesn't get empty. I'd suggest logging out the message ids as they are handled.

  2. You aren't handling paint messages - from msdn: "The PeekMessage function normally does not remove WM_PAINT messages from the queue. WM_PAINT messages remain in the queue until they are processed. However, if a WM_PAINT message has a NULL update region, PeekMessage does remove it from the queue."

Hope this helps.

[Edit] To handle WM_PAINT either call BeginPaint and EndPaint or forward to DefWindowProc

Upvotes: 7

jussij
jussij

Reputation: 10560

Make sure you are processing the WM_PAINT correctly.

By this I mean make sure you are calling BeginPaint and EndPaint from inside the WM_PAINT message, otherwise you will be confusing Windows into thinking your application still needs to be painted.

Upvotes: 4

Naveen
Naveen

Reputation: 73433

PeekMessage will return 0 only if there are no messages in the message queue. Since there are messages to be dispatched in the message queue, it is returning a non-zero value and your else condition is never executed.

Upvotes: 0

Emmanuel Caradec
Emmanuel Caradec

Reputation: 2287

May be there is always a message waiting ?

Upvotes: 0

Related Questions