简体   繁体   English

Windows消息循环

[英]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). 创建窗口,处理消息,在客户端区域中将事件生成到键盘输入,gl canvas可以正常工作(当我强制绘制时)。

The only problem is that message loop never leaves the first if. 唯一的问题是消息循环永远不会离开第一个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;

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. 我建议注销处理后的消息ID。

  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." 您不是在处理绘画消息-来自msdn:“ PeekMessage函数通常不会从队列中删除WM_PAINT消息。WM_PAINT消息会保留在队列中直到被处理。但是,如果WM_PAINT消息的更新区域为NULL,则PeekMessage会从队列中删除它。”

Hope this helps. 希望这可以帮助。

[Edit] To handle WM_PAINT either call BeginPaint and EndPaint or forward to DefWindowProc [编辑]要处理WM_PAINT,请调用BeginPaint和EndPaint或转发到DefWindowProc

Make sure you are processing the WM_PAINT correctly. 确保正确处理WM_PAINT

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. 我的意思是确保您从WM_PAINT消息内部调用BeginPaintEndPaint ,否则您将使Windows误以为您的应用程序仍然需要绘制。

可能总是有消息在等待吗?

PeekMessage will return 0 only if there are no messages in the message queue. 仅当消息队列中没有消息时,PeekMessage才会返回0。 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. 由于消息队列中有要分派的消息,因此它返回一个非零值,并且您的else条件永远不会执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM