SendMessage函数

20 篇文章 1 订阅

SendMessage函数发送指定消息到一个或多个窗口。它将调用指定窗口的窗口处理过程,并且直到窗口过程处理完该消息才返回。

要发送消息后马上返回,可以使用SendMessageCallback函数或SendNotifyMessage函数。要投递一个消息到线程消息队列并马上返回,可以使用PostMessage函数或PostThreadMessage函数。

函数原型:

LRESULT SendMessage(         

HWNDhWnd,

    UINT Msg,

    WPARAMwParam,

    LPARAMlParam

);

参数说明:

hWnd:指定将要接收消息的窗口过程的窗口句柄。如果此参数为的值为HWND_BROADCAST,则消息将被发送到系统中所有顶层窗口,包括无效或不可见的非自身拥有的窗口、被覆盖的窗口和弹出式窗口,但消息不被发送到子窗口。

Msg:指定将要被发送的消息。

wParam:指定附加的消息特定信息。

lParam:指定附加的消息特定信息。

返回值:

返回指定消息的处理结果。该结构依赖于所发送的消息。

注意事项:

需要用HWND_BROADCAST进行通信的应用程序应当使用函数RegisterWindowMessage来为跨应用的通信取得一个唯一的消息。

系统只负责编组系统消息(那些范围在0到WM_USER之间的)。要发送其它消息(那些大于WM_USER的)到另外的过程中,你必须自定义编组(即消息的编号)。

如果指定的窗口是由正在调用的线程创建的,则窗口程序立将即作为子程序调用。如果指定的窗口是由不同线程创建的,则系统切换到该线程并调用恰当的窗口程序。线程间的消息只有在线程执行消息检索代码时才被处理。发送线程被阻塞直到接收线程处理完消息为止。然而,发送消息的线程在等待被发送消息处理完的过程中也会处理到来的非队列消息。为了防止这种情况的发生,可以使用SendMessageTimeout并设置fuFlags参数为SMTO_BLOCK来实现。

 

//原文如下:

The SendMessage function sendsthe specified message to a window or windows. It calls the window procedure forthe specified window and does not return until the window procedure hasprocessed the message.

To send a message andreturn immediately, use the SendMessageCallback or SendNotifyMessage function.To post a message to a thread's message queue and return immediately, use thePostMessage or PostThreadMessage function.

Syntax

LRESULT SendMessage(      

    HWND hWnd,

    UINT Msg,

    WPARAM wParam,

    LPARAM lParam

);

Parameters

hWnd

[in] Handle to thewindow whose window procedure will receive the message. If this parameter isHWND_BROADCAST, the message is sent to all top-level windows in the system,including disabled or invisible unowned windows, overlapped windows, and pop-upwindows; but the message is not sent to child windows.

Msg

[in] Specifies themessage to be sent.

wParam

[in] Specifiesadditional message-specific information.

lParam

[in] Specifiesadditional message-specific information.

Return Value

Thereturn value specifies the result of the message processing; it depends on themessage sent.

Remarks

Applications that need tocommunicate using HWND_BROADCAST should use the RegisterWindowMessage functionto obtain a unique message for inter-application communication.

The system only doesmarshalling for system messages (those in the range 0 to WM_USER). To sendother messages (those above WM_USER) to another process, you must docustom marshalling.

If the specified window wascreated by the calling thread, the window procedure is called immediately as asubroutine. If the specified window was created by a different thread, thesystem switches to that thread and calls the appropriate window procedure.Messages sent between threads are processed only when the receiving threadexecutes message retrieval code. The sending thread is blocked until thereceiving thread processes the message. However, the sending thread willprocess incoming nonqueued messages while waiting for its message to beprocessed. To prevent this, use SendMessageTimeout with SMTO_BLOCK set. Formore information on nonqueued messages, see Nonqueued Messages.

Windows 95/98/Me: SendMessageWis supported by the Microsoft Layer for Unicode (MSLU). To use this, you mustadd certain files to your application, as outlined in Microsoft Layer forUnicode on Windows 95/98/Me Systems.

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值