SendMessage与PostMessage的区别

  • Post author:
  • Post category:其他



SendMessage:

This is function sends the specified message to a window or windows.


SendMessage calls the window procedure for the specified window and



does not return until the window procedure has processed the message.



The PostMessage function, in contrast, posts a message to a thread’s



message queue and returns immediately.

LRESULT SendMessage(
    HWND hWnd,
    UINT Msg,
    WPARAM wParam,
    LPARAM lParam);

Parameters:

hWnd:

[in]Handle to the window whose window procedure will receive the message.

If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system,

including disabled or invisible unowned windows, overlapped windows, and pop-up windows;

but the message is not sent to child windows.

Msg:

[in]Specifies the message to be sent.

wParam:

[in]Specifies additional message-specific information.

lParam:

[in]Specifies additional message-specific information.

Return Values:

The return value specifies the result of the message processing and depends on the message sent.


PostMessage:

The PostMessage function places(posts) a message in the message,

queue associated with the thread that created the specified window

and returns without waiting for the thread to process the message.

To post a message in the message queue associate with a thread,

use the PostThreadMessage function.

Bool PostMessage(
    HWND hWnd,      // handle to destination window
    UINT Msg,       // message
    WPARAM wParam,  // first message parameter
    LPARAM lParam   // second message parameter
);

Parameters:

hWnd

[in] Handle to the window whose window procedure is receive the message.

The following values have special meanings.

HWND_BROADCAST:

The message is posted to all top-level windows in the system,

including disabled or invisible unowned windows, overlapped windows,

and pop-up windows. The message is not posted to child windows.

NULL:

The function behaves like a call to PostThreadMessage with the

dwThreadId parameter set to the identifier of the current thread.

Msg

[in] Specifies the message to be posted.

wParam

[in] Specifies additional message-specific information.

lParam

[in] Specifies additional messgae-specific information.

Return Values:

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

以上是MSDN关于SendMessage和PostMessage两个函数的介绍,从中可以看到有这么一句话,

SendMessage calls the window procedure for the specified window anddoes not return until the window procedure has processed the message.The PostMessage function, in contrast, posts a message to a thread’smessage queue and returns immediately.

意思就是


SendMessage函数将消息发送到窗口代号后直到消息被处理后才会返回,而PostMessage函数则仅将消息放入消息队列便立即返回,换句话讲,SendMessage属于同步消息传递而PostMessage为异步消息传递。



版权声明:本文为flyingsbird原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。