Windows 消息处理 - PeekMessage

来源:互联网 发布:车架号查询软件 编辑:程序博客网 时间:2024/05/15 04:42

当运行复杂的计算或循环的时候,为了使windows程序不产生假死现象(不响应任何消息,cpu100%占用).要在适当地方添下如下消息处理机能。


// Process the thread's window message

MSG msg;
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {

        if(msg.message== WM_QUIT)    //Check for the WM_QUIT message

              break;

        // Send the message to the windowprocedure
        TranslateMessage(&msg);
        DispatchMessage(&msg);
}


另外说明:

PeekMessage和GetMessage的区别
  1.
GetMessage将等到有合适的消息时才返回,而PeekMessage只是撇一下消息队列.
  2.
GetMessage会将消息从队列中删除,而PeekMessage可以设置最后一个参数wRemoveMsg来决    定是否将消息保留在队列中.