PeekMessage Function

The PeekMessage function checks to see whether a message, placed in the message queue by a posted thread or the thread, is available. If a message is available, PeekMessage copies it into the structure pointed to by lpMsg and returns a nonzero value.

If no message is available, PeekMessage returns zero. PeekMessage never waits for a message to come into the queue.

A message queue is associated with each thread and each process that has been associated with the input system.

Syntax

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

Parameters

Parameter Description
lpMsg A pointer to a MSG structure that receives message information.
hWnd A handle to the window whose messages are to be examined. This parameter can be NULL. If it is NULL, PeekMessage examines messages for any window that is associated with the calling thread.
wMsgFilterMin The minimum value of a message to be retrieved. This parameter is ignored if hWnd is NULL.
wMsgFilterMax The maximum value of a message to be retrieved. This parameter is ignored if hWnd is NULL.
wRemoveMsg Specifies how messages are to be removed from the queue.
This parameter can be one or more of the following values:
  • PM_NOREMOVE (0x0000) - Messages are removed from the queue after processing by the system.
  • PM_REMOVE (0x0001) - Messages are removed from the queue after processing by the system.
  • PM_NOYIELD (0x0002) - Prevents the system from yielding if called from a multithreaded application.
  • PM_QS_SENDMESSAGE (0x0008) - Retrieves messages that are posted in response to a SendMessage call.

Return Value

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

Remarks

The PeekMessage function is typically used in the main message loop of an application.

If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the calling thread. This includes messages associated with child windows, but not messages from other threads.

The wMsgFilterMin and wMsgFilterMax parameters can be used to specify a range of messages to retrieve. If both are zero, all messages are retrieved.

Example

C++
MSG msg;
BOOL bRet;

// Standard message loop
while( (bRet = PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )) != 0 )
{
    if (bRet == -1) // Handle error
    {
        // Error handling code
        break;
    }

    // If it's a quit message, exit the loop
    if(msg.message == WM_QUIT)
    {
        PostQuitMessage(msg.wParam);
        break;
    }

    // Translate message
    TranslateMessage(&msg);

    // Dispatch message
    DispatchMessage(&msg);
}

// Process remaining messages in the queue if any
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

Requirements

Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header winuser.h (include Windows.h)
Library User32.lib
DLL User32.dll

See Also