Export (0) Print
Expand All
25 out of 44 rated this helpful - Rate this topic

PeekMessage function

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

Syntax


BOOL WINAPI PeekMessage(
  _Out_     LPMSG lpMsg,
  _In_opt_  HWND hWnd,
  _In_      UINT wMsgFilterMin,
  _In_      UINT wMsgFilterMax,
  _In_      UINT wRemoveMsg
);

Parameters

lpMsg [out]

Type: LPMSG

A pointer to an MSG structure that receives message information.

hWnd [in, optional]

Type: HWND

A handle to the window whose messages are to be retrieved. The window must belong to the current thread.

If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed.

If hWnd is -1, PeekMessage retrieves only messages on the current thread's message queue whose hwnd value is NULL, that is, thread messages as posted by PostMessage (when the hWnd parameter is NULL) or PostThreadMessage.

wMsgFilterMin [in]

Type: UINT

The value of the first message in the range of messages to be examined. Use WM_KEYFIRST (0x0100) to specify the first keyboard message or WM_MOUSEFIRST (0x0200) 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]

Type: UINT

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]

Type: UINT

Specifies how messages are to be handled. This parameter can be one or more of the following values.

ValueMeaning
PM_NOREMOVE
0x0000

Messages are not removed from the queue after processing by PeekMessage.

PM_REMOVE
0x0001

Messages are removed from the queue after processing by PeekMessage.

PM_NOYIELD
0x0002

Prevents the system from releasing any thread that is waiting for the caller to go idle (see WaitForInputIdle).

Combine this value with either PM_NOREMOVE or PM_REMOVE.

 

By default, all message types are processed. To specify that only certain message should be processed, specify one or more of the following values.

ValueMeaning
PM_QS_INPUT
(QS_INPUT << 16)

Process mouse and keyboard messages.

PM_QS_PAINT
(QS_PAINT << 16)

Process paint messages.

PM_QS_POSTMESSAGE
((QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER) << 16)

Process all posted messages, including timers and hotkeys.

PM_QS_SENDMESSAGE
(QS_SENDMESSAGE << 16)

Process all sent messages.

 

Return value

Type:

Type: BOOL

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 the hWnd parameter or any of its children as specified by the IsChild function, and within the range of message values given by the wMsgFilterMin and wMsgFilterMax parameters. Note that an application can only use the low word in the wMsgFilterMin and wMsgFilterMax parameters; the high word is reserved for the system.

Note that PeekMessage always retrieves WM_QUIT messages, no matter which values you specify for wMsgFilterMin and wMsgFilterMax.

During this call, the system delivers pending, nonqueued messages, that is, messages sent to windows owned by the calling thread using the SendMessage, SendMessageCallback, SendMessageTimeout, or SendNotifyMessage function. Then the first queued message that matches the specified filter is retrieved. The system may also process internal events. If no filter is specified, 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 and wMsgFilterMax parameters.

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.

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.

Examples

For an example, see Examining a Message Queue.

Requirements

Minimum supported client

Windows 2000 Professional [desktop apps only]

Minimum supported server

Windows 2000 Server [desktop apps only]

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

Unicode and ANSI names

PeekMessageW (Unicode) and PeekMessageA (ANSI)

See also

Reference
GetMessage
IsChild
MSG
WaitMessage
Conceptual
Messages and Message Queues
Other Resources
WaitForInputIdle

 

 

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.