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

GetMessage function (Windows)

2013年12月06日 ⁄ 综合 ⁄ 共 5141字 ⁄ 字号 评论关闭

原文地址::http://msdn.microsoft.com/ZH-CN/library/windows/desktop/ms644936(v=vs.85).aspx

41(共 68)对本文的评价是有帮助 评价此主题

Retrieves a message from the calling thread's message queue. The function dispatches incoming sent messages until a posted message is available for retrieval.

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

Syntax

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

Parameters

lpMsg [out]

Type: LPMSG

A pointer to an MSG structure that
receives message information from the thread's message queue.

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 NULLGetMessage 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, GetMessage 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 hWndparameter is NULL) or PostThreadMessage.

wMsgFilterMin [in]

Type: UINT

The integer value of the lowest message value to be retrieved. Use WM_KEYFIRST (0x0100) to specify the first keyboard message or WM_MOUSEFIRST (0x0200) to specify the first mouse message.

Use WM_INPUT here and in wMsgFilterMax to
specify only the WM_INPUT messages.

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

wMsgFilterMax [in]

Type: UINT

The integer value of the highest message value to be retrieved. Use WM_KEYLAST to specify the last keyboard message or WM_MOUSELAST to specify the last mouse message.

Use WM_INPUT here and in wMsgFilterMin to
specify only the WM_INPUT messages.

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

Return value

Type:

Type: BOOL

If the function retrieves a message other than WM_QUIT,
the return value is nonzero.

If the function retrieves the WM_QUIT message,
the return value is zero.

If there is an error, the return value is -1. For example, the function fails if hWnd is an invalid window handle or lpMsg is an invalid pointer. To get extended error information, call GetLastError.

Because the return value can be nonzero, zero, or -1, avoid code like this:

while (GetMessage( lpMsg, hWnd, 0, 0)) ...

The possibility of a -1 return value means that such code can lead to fatal application errors. Instead, use code like this:

BOOL bRet;

while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
}

Remarks

An application typically uses the return value to determine whether to end the main message loop and exit the program.

The GetMessage function retrieves messages associated with the window identified by the hWndparameter 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 GetMessage always retrieves WM_QUIT messages,
no matter which values you specify forwMsgFilterMin and wMsgFilterMax.

During this call, the system delivers pending, nonqueued messages, that is, messages sent to windows owned by the calling thread using the SendMessageSendMessageCallbackSendMessageTimeout,
orSendNotifyMessage 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 wMsgFilterMaxparameters.

GetMessage does not remove WM_PAINT messages
from the queue. The messages remain in the queue until processed.

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 in the debugger mode, the system does not generate a ghost window.

Examples

For an example, see Creating a Message Loop.

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

GetMessageW (Unicode) and GetMessageA (ANSI)

See also

Reference
IsChild
MSG
PeekMessage
PostMessage
PostThreadMessage
WaitMessage
Conceptual
Messages and Message Queues

 

 

Send
comments about this topic to Microsoft

Build date: 10/16/2012

//=================================

备注::

1>看来很多WINDOWS API函数都别有洞天啊!不看不知道,一细看吓一跳,原来自己对WINDOWS下基本的API了解的也是这样的肤浅,

实在是惭愧啊!

2>对东西都把握不准,谈何把它用好用准呢?对WINDOWS下基本的API都只知道个模糊的概念,看来我的WINDOWS编程还没有入门啊!


3>

The possibility of a -1 return value means that such code can lead to fatal application errors. Instead, use code like this:

BOOL bRet;

while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
}

抱歉!评论已关闭.