PostMessage函数

20 篇文章 1 订阅

PostMessage函数放置(投递)消息到创建指定窗口的线程所关联的消息队列中,并且不会等待相应线程处理消息,而直接返回。

要将消息投递到线程所关联的消息队列,可以使用PostThreadMessage函数。

函数原型:

BOOL PostMessage(        

 HWND hWnd,

    UINT Msg,

    WPARAMwParam,

    LPARAMlParam

);

参数说明:

hWnd:指定将要接收消息的窗口过程的窗口句柄。下面的值有其特殊含义:

HWND_BROADCAST

消息将被发送到系统中所有顶层窗口,包括无效或不可见的非自身拥有的窗口、被覆盖的窗口和弹出式窗口,但消息不被发送到子窗口。

NULL

该函数的行为和调用PostThreadMessage函数时将dwThreadId参数设置为当前线程id一致。

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

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

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

返回值:

如果函数执行成功,返回值为非0。

如果函数执行失败,返回值为0。可以调用GetLastError函数来获取更多的错误信息。

注意事项:

在消息队列中的消息将被GetMessage或PeekMessage函数检索。

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

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

如果你使用异步消息函数(PostMessage,SendNotifyMessage,SendMessageCallback)发送WM_USER之下范围的消息(系统消息),则它的消息参数中不能包括指针。否则,该操作将会失败。函数将会在接收线程有机会处理该消息之前返回,并且发送者会在对应内存被使用之前释放它。

不要使用PostMessage来投递WM_QUIT消息;使用PostQuitMessage函数来做这件事。

在 Windows 2000/XP 里,每个消息队列最多只能存放 10,000 个 Post的消息,这样的设置应该是足够大的。如果你的应用超过了这一限制,为了避免消耗过多的系统资源,该应用应该被重新设计。为了适应这项限制,可以通过如下的注册表项来进行修改:HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/WindowsNT/CurrentVersion/Windows/USERPostMessageLimit,最小的可接受值是4000。

 

//原文如下:

The PostMessage function places(posts) a message in the message queue associated with the thread that createdthe specified window and returns without waiting for the thread to process themessage.

To post a message in themessage queue associate with a thread, use the PostThreadMessage function.

Syntax

BOOL PostMessage(      

    HWND hWnd,

    UINT Msg,

    WPARAM wParam,

    LPARAM lParam

);

Parameters

hWnd

[in] Handle to thewindow whose window procedure is to receive the message. The following valueshave special meanings.

HWND_BROADCAST

The message is posted toall top-level windows in the system, including disabled or invisible unownedwindows, overlapped windows, and pop-up windows. The message is not posted tochild windows.

NULL

The function behaves like acall to PostThreadMessage with the dwThreadId parameter set tothe identifier of the current thread.

Msg

[in] Specifies themessage to be posted.

wParam

[in] Specifiesadditional message-specific information.

lParam

[in] Specifiesadditional message-specific information.

Return Value

If the function succeeds,the return value is nonzero.

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

 

Remarks

Messages in a message queueare retrieved by calls to the GetMessage or PeekMessage function.

Applications that need tocommunicate using HWND_BROADCAST should use the RegisterWindowMessage function toobtain 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 you send a message inthe range below WM_USER to the asynchronous message functions (PostMessage,SendNotifyMessage, and SendMessageCallback), its message parameters cannotinclude pointers. Otherwise, the operation will fail. The functions will returnbefore the receiving thread has had a chance to process the message and thesender will free the memory before it is used.

Do not post the WM_QUITmessage using PostMessage;use the PostQuitMessage function.

Windows 2000/XP: There is a limit of 10,000posted messages per message queue. This limit should be sufficiently large. Ifyour application exceeds the limit, it should be redesigned to avoid consumingso many system resources. To adjust this limit, modify the following registrykey:

HKEY_LOCAL_MACHINE

SOFTWARE

Microsoft

Windows NT

CurrentVersion

Windows

USERPostMessageLimit

The minimum acceptable value is 4000.

Windows 95/98/Me: PostMessageWis 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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值