关于SendMessage和PostMessage的一些说法

先说两者API说明,后面是两者的一些区别

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)

你可能感兴趣的:(thread,windows,function,Parameters,returning,Pointers)