SendMessage 和 PostMessage

1. SendMessage函数要等到消息被处理后才返回

    PostMessage 消息发出后马上返回

2. PostMessage发向目标窗口的消息一定会进入消息队列

    SendMessage向同一线程的窗口发消息,不会进入消息队列,

    SendMessage向其他线程的窗口发消息,则会进入消息队列,

3. PostMessage的返回值表示PostMessage函数执行是否正确,

   SendMessage的返回值表示其他程序处理消息后的返回值

=.

PostMessage的API说明

Unit
Windows.Pas

Syntax
PostMessage(
hWnd: HWND;   {a handle to a window}
Msg: UINT;   {the identifier of the message to send}
wParam: WPARAM;  {a 32 bit message specific value}
lParam: LPARAM   {a 32 bit message specific value}
): BOOL;    {returns TRUE or FALSE}

Description
This function places the indicated message in the message queue of the thread that owns the specified window, returning immediately without waiting for the message to be processed. Caution is advised when sending a message whose parameters contain pointers, as the function will return before the thread associated with the specified window has a chance to process the message and the pointers could be freed before they are used.

Parameters
hWnd: A handle to the window whose window procedure is to receive the specified message. If this parameter is set to zero, PostMessage functions exactly like a call to the PostThreadMessage function with the idThread parameter set to the identifier of the calling thread. If this parameter is set to HWND_BROADCAST, the message is sent to all top level windows in the system, including disabled and invisible windows. The message is not sent to child windows. Applications that need to send a user defined message to other applications using HWND_BROADCAST should use the RegisterWindowMessage to obtain a unique message identifier.

Msg: The identifier of the message to send.

wParam: A 32 bit value dependent on the message being sent.

lParam: A 32 bit value dependent on the message being sent.

Return Value
If the function succeeds, it returns TRUE; otherwise it returns FALSE. To get extended error information, call the GetLastError function.

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

SendMessage的API说明

Unit
Windows.Pas

Syntax
SendMessage(
hWnd: HWND;    {a handle to a window}
Msg: UINT;    {the identifier of the message to send}
wParam: WPARAM;   {a 32 bit message specific value}
lParam: LPARAM    {a 32 bit message specific value}
): LRESULT;    {returns a message specific result}

Description
This function sends the specified message to the window procedure of the indicated window, and does not return until the called window procedure has processed the message. If the specified window belongs to the calling thread, that window's window procedure is called immediately as a subroutine. However, if the window belongs to a different thread, Windows switches to that thread, sending the message to the appropriate window procedure, and the thread sending the message is blocked until the receiving thread processes the message.

Parameters
hWnd: A handle to the window whose window procedure is to receive the specified message. If this parameter is set to HWND_BROADCAST, the message is sent to all top level windows in the system, including disabled and invisible windows. The message is not sent to child windows. Applications that need to send a user defined message to other applications using HWND_BROADCAST should use the RegisterWindowMessage to obtain a unique message identifier.

Msg: The identifier of the message to send.

wParam: A 32 bit value dependent on the message being sent.

lParam: A 32 bit value dependent on the message being sent.

Return Value
If the function succeeds, it returns a message specific value indicating the result of the message processing. If the function fails, it returns zero.

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

关于两者的说法:

1、SendMessage()和PostMessage().这两个函数几乎是一样的,它们的区别是:
SendMessage()直接把一个消息发给窗口过程,等消息被处理后才返回。Postmessage()只是把消息发送到消息队列,完成后即返回。

2、(http://xsz.blogdriver.com/xsz/233174.html)

2.1、当调用SendMessage时,接收消息的线程的QS_SENDMESSAGE标志被设置,系统调用相应的窗口过程来处理这个消息。 GetMessage和PeekMessage函数在内部进行这种处理。如果在“发送消息队列”中没有消息了,QS_SENDMESSAGE标志就被关闭。“发送消息队列”中可能有几个Send过来的消息。例如,几个线程同时向一个窗口发送消息。

2.2、当调用PostMessage时,函数GetMessage或PeekMessage填充传递给它们的Msg结构,函数返回。再调用DispatchMessage,让相应的窗口过程来处理消息。

2.3、当调用SendMessage时,发送线程和接收线程是同一个线程的时候,SendMessag很简单,只是调用指定窗口的窗口过程。但当发送线程和接收线程不是同一个线程时,麻烦就大了。因为发送线程和接收线程运行在不同的地址空间中,因此不能访问接受线程中相应窗口过程的代码和数据。其实这时发送线程要挂起,当接收线程处理Send过来的消息时,发送线程被设置为空闲(idle)状态。在发送的消息处理完成后,窗口过程的返回值被登记到发送线程的应答消息队列中。发送线程被唤醒,取出包含在应答消息队列中的返回值。这个返回值就是调用SendMessage的返回值,这时,发送线程继续运行。

2.4、WM_PAINT和WM_TIMER这两个消息的优先级非常低,最低的是WM_TIMER。

2.5、WM_COPYDATA只能Send,不能Post。

3、(http://blog.xoyo.com/space.php?do=article&aid=539485)

PS:

下表列出了Windows95中Windows消息值的范围。

常 量 值 消息值范围 意 义
WM_USER 0x0400 0x0000-0x03FF Windows消息
    0x0400-0x7FFF 用户自定义的消息
    0x8000-0xBFFF Windows保留值
    0xC000-0xFFFF 供应用使用的字符串消息


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jifengszf/archive/2009/02/10/3872743.aspx

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值