日期:2014-05-17 浏览次数:20843 次
Windows 有很多的 “ 空闲时间 ”,在这期间所有的消息队列都是空的,Windows 就在等待键盘或者鼠标的输入。 那么能否在空闲期间从某种程度上获取控制并绘制随机矩形,而一旦有消息加载到程序的消息队列,就释放控制呢?这正是 PeekMessage 函数的 ” 用武之地 “
The PeekMessage function dispatches incoming sent messages, checks the thread message queue for a posted message, and retrieves the message (if any exist).
BOOL PeekMessage( LPMSG lpMsg, // message information HWND hWnd, // handle to window UINT wMsgFilterMin, // first message UINT wMsgFilterMax, // last message UINT wRemoveMsg // removal options );
Value | Meaning |
---|---|
PM_NOREMOVE | Messages are not removed from the queue after processing by PeekMessage. |
PM_REMOVE | Messages are removed from the queue after processing by PeekMessage. |
You can optionally combine the value PM_NOYIELD with either PM_NOREMOVE or PM_REMOVE. This flag prevents the system from releasing any thread that is waiting for the caller to go idle (see WaitForInputIdle).
By default, all message types are processed. To specify that only certain message should be processed, specify one of more of the following values.
Value | Meaning |
---|---|
PM_QS_INPUT | Windows 98, Windows 2000: Process mouse and keyboard messages. |
PM_QS_PAINT | Windows 98, Windows 2000: Process paint messages. |
PM_QS_POSTMESSAGE | Windows 98, Windows 2000: Process all posted messages, including timers and hotkeys. |
PM_QS_SENDMESSAGE | Windows 98, Windows 2000: Process all sent messages. |
If a message is available, the return value is nonzero.
If no messages are available, the return value is zero.
有了这个 PeekMessage 函数,我们只需简单修改一下正常的消息循环,如下:
while (TRUE)
{
if(PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);