091020(星期二)MFC线程消息循环3,GetMessage函数msdn描述

GetMessage Function

Windows的系统函数,已经超出MFC的范畴。

Minimum DLL Version user32.dll

Header Declared in Winuser.h, include Windows.h

Import library User32.lib

--------------------------------------------------------------------------------

The GetMessage function 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 GetMessage(         

LPMSG lpMsg,

    HWND hWnd,

    UINT wMsgFilterMin,

    UINT wMsgFilterMax

);

Parameters

lpMsg

[out] Pointer to an MSG structure that receives message information from the thread's message queue.

 

typedef struct {

    HWND hwnd; //Handle to the window whose window procedure receives the message

    UINT message;// Specifies the message identifier. Applications can only use the low word; the high word is reserved by the system.

    WPARAM wParam;// Specifies additional information about the message. The exact meaning depends on the value of the message member.

    LPARAM lParam; // 同上

    DWORD time; // Specifies the time at which the message was posted

    POINT pt; //Specifies the cursor position, in screen coordinates, when the message was posted

} MSG, *PMSG;

 

hWnd

[in] Handle to the window whose messages are to be retrieved. The window must belong to the calling thread. The NULL value has a special meaning:

NULLCWinThread::PumpMessage函数中正是使用了NULL hWnd

GetMessage retrieves messages for any window that belongs to the calling thread and thread messages posted to the calling thread using the PostThreadMessage function.

获取Thread的所以消息,包括使用PostThreadMessage传递的消息。

 

wMsgFilterMin

[in] Specifies the integer value of the lowest message value to be retrieved. Use WM_KEYFIRST to specify the first keyboard message or WM_MOUSEFIRST to specify the first mouse message.

Windows XP: 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] Specifies 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.

Windows XP: 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

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.

 

Warning 

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, NULL, 0, 0 )) != 0)

{

    if (bRet == -1) // 需要对返回值进行区分,例子举得这么好,自己MFCCWinThread::PumpMessage的实现也没严格这么判。基于GetMessage(&m_msgCur, NULL, NULL, NULL)一定不会返回-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 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最低的2个字节 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 for wMsgFilterMin and wMsgFilterMax.

 

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: 按以下顺序Get

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.

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

 

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

 

 

Example 例子明天继续看

For an example, see Creating a Message Loop.

 

Function Information

 

Minimum DLL Version user32.dll

Header Declared in Winuser.h, include Windows.h

Import library User32.lib

Minimum operating systems Windows 95, Windows NT 3.1

Unicode Implemented as ANSI and Unicode versions. 

 

See Also

 

Messages and Message Queues Overview, IsChild, MSG, PeekMessage, PostMessage, PostThreadMessage, WaitMessage

 

--------------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值