现在的位置: 首页 > 综合 > 正文

while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))

2013年10月03日 ⁄ 综合 ⁄ 共 3829字 ⁄ 字号 评论关闭

项目中用到了好多这个消息循环,很是困惑。遂百度之,难道一个窗口下可以有多个消息循环吗?百度结果都是介绍和GetMessage区别的,正经的说这个有啥作用的,少之又少。不过有一篇英文的不错,这是出处,可以参考一下

http://blog.csdn.net/scofieldlau/article/details/1965517#t0

。奈何我的疑惑仍然为解。最后查看MSDN,清晰一二

原文

Unlike the GetMessage function, the PeekMessage function does not wait for a message to be placed in the queue before returning.

PeekMessage retrieves only messages associated with the window identified by thehWnd parameter or any of its children as specified by theIsChild function, and within the range of message values given by thewMsgFilterMin
andwMsgFilterMax parameters. If hWnd is NULL,PeekMessage retrieves messages for any window that belongs to the current thread making the call. (PeekMessage does not retrieve messages for windows that belong
to other threads.) IfhWnd is –1, PeekMessage only returns messages with ahWnd value of NULL, as posted by thePostThreadMessage function. IfwMsgFilterMin andwMsgFilterMax are both zero,PeekMessage
returns all available messages (that is, no range filtering is performed).

The WM_KEYFIRST and WM_KEYLAST constants can be used as filter values to retrieve all keyboard messages; the WM_MOUSEFIRST and WM_MOUSELAST constants can be used to retrieve all mouse messages.

The PeekMessage function does not remove WM_PAINT messages from the queue. WM_PAINT messages remain in the queue until they are processed.

翻译

PeekMesssge只得到那些与参数hWnd标识的窗口相联系的消息或被lsChild确定为其子窗口相联系的消息,并且该消息要在由参数wMsgFiterMin和wMsgFiherMax确定的范围内。如果hWnd为NULL,则PeekMessage接收属于当前调用线程的窗口的消息(PeekMessage不接收属于其他线程的窗口的消息)。如果hWnd为-1,PeekMessage只返回hWnd值为NULL的消息,该消息由函数PostThreadMessage寄送。如果wMsgFilterMin和wMsgFilterMax都为零,PeekMessage返回所有可得的消息(即,无范围过滤)。

常数WM_KEYFIRST和WM_KEYLAST可作为过滤值取得所有键盘消息;常数WM_MOUSEFIRST和WM_MOUSELAST可用来接收所有的鼠标消息。
PeekMessage通常不从队列里清除WM_PAINT消息。该消息将保留在队列里直到处理完毕。但如果WM_PAINT消息有一个空更新区,PeekMessage将从队列里清除WM_PAINT消息。
终于有点明白了,

PeekMessage接收属于当前调用线程的窗口的消息(PeekMessage不接收属于其他线程的窗口的消息)。如果hWnd为-1,PeekMessage只返回hWnd值为NULL的消息,该消息由函数PostThreadMessage寄送。如果wMsgFilterMin和wMsgFilterMax都为零,PeekMessage返回所有可得的消息(即,无范围过滤)。

这句话很关键,在线程运行过程中,为了能够处理消息,我们可以添加一个消息循环,来防止线程的死锁。可不可以这么了解呢。

<Windows 程序设计>中的第925页有这么一段话

一个程序可以在运行的长时间任务中掺入PeekMessage调用。这样,我们既可以在没有新消息的时候保持长时间任务的运行,又可以对新的消息作出及时的反应。

下面是一些资料整理

I’d like to make my application repaint during one of my (long) loading process. If I Open another app in front of my loading app it won’t bee repainted until the loading process is done.
 
How can I handle that in MFC C++?
 
You can simulate the VB DoEvents() function
using this:

 
bool DoEvents()
{
 MSG msg;
 while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
 {
  if (msg.message == WM_QUIT)
  {
   return false;
  }
  if (!AfxGetApp()->PreTranslateMessage(&msg))
  {
   ::TranslateMessage(&msg);
   ::DispatchMessage(&msg);
  }
 }
 return true;
}

Does each application have it's own message queue? Or is it one shared queue.
 
The VB function DoEvents()
 
and the C++ code
 
MSG msg;
while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg) ;
DispatchMessage(&msg);
}
 
Would indicate that there is one queue.
Roll eyes | :rolleyes: 


Each thread that creates a window is allotted its own message queue, and must include a message pump like the one you've litsed in order to process messages for that thread.
 
Think thread, not application. Here's a snip from the "Multiple Threads in the User Interface" article in the MSDN:
 
Changes to the Message Loop
 
"Applications with multiple threads must include a message loop in each thread that creates a window. The message loop and window procedure for a window must be processed by the thread that created the window. If the message loop does not reside in the same
thread that created the window, the DispatchMessage function will not get messages for the window. As a result, the window will appear but won't show activation and won't repaint, be moved, receive mouse messages, or generally work as you expect it to."
 
later... as usual... 
Hmmm - maybe this is more helpful - from the PSDK docs on windowing and messages:
 
"The system maintains a single system message queue and one thread-specific message queue for each GUI thread. To avoid the overhead of creating a message queue for non-GUI threads, all threads are created initially without a message queue. The system creates
a thread-specific message queue only when the thread makes its first call to one of the Win32 User or GDI functions."


希望看到这篇文章的你能够分享你的体会。突然觉得有一种编程境界叫做直觉,呵呵

抱歉!评论已关闭.