PeekMessage函数

来源:互联网 发布:mysql mediumtext 编辑:程序博客网 时间:2024/06/07 00:32

PeekMessage函数分发到来的“发送消息”(非队列消息),在线程消息队列中查找“投递消息”,并返回找到的消息(如果有的话)。

函数原型:

BOOL PeekMessage(      

    LPMSG lpMsg,

    HWND hWnd,

    UINT wMsgFilterMin,

    UINT wMsgFilterMax,

    UINT wRemoveMsg

);

参数说明:

lpMsg:指向一个MSG结构的指针,该结构中保存着从线程消息队列中取得的消息信息。

hWnd:被测试消息的相关窗口句柄。该窗口必须属于正在调用的线程。

如果hWnd值为NULL:PeekMessage检索任何属于调用线程窗口的消息。如果hWnd的值为INVALID_HANDLE_VALUE,PeekMessage会检索那些hWnd值为NULL的消息,并作为由PostThreadMessage投递的消息来对待。

wMsgFilterMin:指定被检索的最小消息值的整数。使用WM_KEYFIRST指定第一个键盘消息或WM_MOUSEFIRST指定第一个鼠标消息。

wMsgFilterMax:指定被检索的最大消息值的整数。使用WM_KEYLAST指定最后的键盘消息或WM_MOUSEFIRST指定最后的鼠标消息。

如果将wMsgFilterMin和wMsgFilterMax都指定为0,GetMessage函数会返回所有可用的消息(此时并没有设置消息的筛选范围)。

wRemoveMsg:指定消息的处理方式。该参数的值可以为下列的值之一。

PM_NOREMOVE

消息经PeekMessage处理后并不从消息队列中移除。

PM_REMOVE

消息经PeekMessage处理后从消息队列中移除。

你可以有选择性的组合PM_NOYIELD与PM_NOREMOVE或PM_REMOVE。PM_NOYIELD标志将阻止系统释放等待调用程序空闲的线程。

默认情况下,所有类型的消息都将被处理。指定以下一个或多个值可以选择要处理的消息类型。

PM_QS_INPUT

Windows 98/Me,Windows 2000/XP: 处理鼠标和键盘消息。

PM_QS_PAINT

Windows 98/Me,Windows 2000/XP:处理绘图消息。

PM_QS_POSTMESSAGE

Windows 98/Me,Windows 2000/XP:处理所有投递消息,包括定时器和热键消息。

PM_QS_SENDMESSAGE

Windows 98/Me,Windows 2000/XP: 处理所有发送消息。

返回值:

如果有可用的消息,则返回值为非0;

如果没有可用的消息,则返回值为0;

注意事项:

PeekMessage函数检索由hWnd参数标识的关联窗体,或者由IsChild函数指定的它们子窗体的消息,并且消息范围在wMsgFilterMin和wMsgFilterMax参数指定值之间的消息。注意,应用只能用wMsgFilterMin和wMsgFilterMax参数的低字位,高字位是为系统保留的。

注意,不管在wMsgFilterMin和wMsgFilterMax指定的范围如何,PeekMessage函数总是检索会WM_QUIT消息。

在函数调用期间,系统用SendMessage、SendMessageCallback,、SendMessageTimeout或者SendNotifyMessage函数发送待处理的、非队列的消息给属于当前线程的窗口。系统也可能处理内部事件。消息的处理顺序如下:

 Sentmessages(发送消息)

 Postedmessages(投递消息)

 输入(硬件)消息或者系统内部事件

 Sentmessages (again)(发送消息)

WM_PAINT消息

WM_TIMER消息

在投递消息之前检索输入消息,可以利用wMsgFilterMin和wMsgFilterMax参数做检索设置。

PeekMessage通常不会从消息队列中移除WM_PAINT消息。该消息将一直存在于消息队列中直到被处理。但是,如果WM_PAINT消息的更新区域为空,则PeekMessage会将其从消息队列中移除。

 

//原文如下:

The PeekMessage function dispatches incoming sent messages, checks the thread message queue for a posted message, and retrieves the message (if any exist).

Syntax

BOOL PeekMessage(          LPMSG lpMsg,    HWND hWnd,    UINT wMsgFilterMin,    UINT wMsgFilterMax,    UINT wRemoveMsg);

Parameters

lpMsg
[out] Pointer to an MSG structure that receives message information.
hWnd
[in] Handle to the window whose messages are to be examined. The window must belong to the current thread.

If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread. IfhWnd is INVALID_HANDLE_VALUE,PeekMessage retrieves messages whosehWnd value is NULL, as posted by the PostThreadMessage function.

wMsgFilterMin
[in] Specifies the value of the first message in the range of messages to be examined. Use WM_KEYFIRST to specify the first keyboard message or WM_MOUSEFIRST to specify the first mouse message.

If wMsgFilterMin and wMsgFilterMax are both zero, PeekMessage returns all available messages (that is, no range filtering is performed).

wMsgFilterMax
[in] Specifies the value of the last message in the range of messages to be examined. Use WM_KEYLAST to specify the last keyboard message or WM_MOUSELAST to specify the last mouse message.

If wMsgFilterMin and wMsgFilterMax are both zero, PeekMessage returns all available messages (that is, no range filtering is performed).

wRemoveMsg
[in] Specifies how messages are handled. This parameter can be one of the following values.
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 or more of the following values.

PM_QS_INPUT
Windows 98/Me, Windows 2000/XP: Process mouse and keyboard messages.
PM_QS_PAINT
Windows 98/Me, Windows 2000/XP: Process paint messages.
PM_QS_POSTMESSAGE
Windows 98/Me, Windows 2000/XP: Process all posted messages, including timers and hotkeys.
PM_QS_SENDMESSAGE
Windows 98/Me, Windows 2000/XP: Process all sent messages.

Return Value

If a message is available, the return value is nonzero.

If no messages are available, the return value is zero.



Remarks

PeekMessage retrieves messages associated with the window identified by thehWnd parameter or any of its children as specified by the IsChild function, and within the range of message values given by thewMsgFilterMin and wMsgFilterMax parameters. Note that an application can only use the low word in thewMsgFilterMin andwMsgFilterMax parameters; the high word is reserved for the system.

Note that PeekMessage always retrieves WM_QUIT messages, no matter which values you specify forwMsgFilterMin andwMsgFilterMax.

During this call, the system delivers pending messages that were sent to windows owned by the calling thread using the SendMessage, SendMessageCallback, SendMessageTimeout, or SendNotifyMessage function. The system may also process internal events. Messages are processed in the following order:

  • Sent messages
  • Posted messages
  • Input (hardware) messages and system internal events
  • Sent messages (again)
  • WM_PAINT messages
  • WM_TIMER messages

To retrieve input messages before posted messages, use the wMsgFilterMin andwMsgFilterMax parameters.

The PeekMessage function normally does not removeWM_PAINT messages from the queue.WM_PAINT messages remain in the queue until they are processed. However, if aWM_PAINT message has a NULL update region,PeekMessage does remove it from the queue.

Windows XP: If a top-level window stops responding to messages for more than several seconds, the system considers the window to be not responding and replaces it with a ghost window that has the same z-order, location, size, and visual attributes. This allows the user to move it, resize it, or even close the application. However, these are the only actions available because the application is actually not responding. When an application is being debugged, the system does not generate a ghost window.

Windows 95/98/Me: PeekMessageW is supported by the Microsoft Layer for Unicode (MSLU). To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems. 


 

 

 

 

0 0
原创粉丝点击