关于PostMessage消息发送 ( 积分: 100 )

  • 主题发起人 主题发起人 tonmy
  • 开始时间 开始时间
T

tonmy

Unregistered / Unconfirmed
GUEST, unregistred user!
下面程序通过PostMessage发送,窗体的WndProc收不到消息,Application.OnMessage则收到;用SendMessage发送时,则相反,但SendMessage发送会造成阻塞,有什么办法发一个消息,程序所有窗口都能收到,同时消息的处理不影响发送消息的线程;不要告诉我循环所有窗口一个个发,因为我不知道有多不个窗口打开着!<br><br>unit&nbsp;Unit1;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs,&nbsp;StdCtrls,&nbsp;AppEvnts;<br><br>type<br>&nbsp;&nbsp;TForm1&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;Button1:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;ApplicationEvents1:&nbsp;TApplicationEvents;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;FormCreate(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button1Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;ApplicationEvents1Message(var&nbsp;Msg:&nbsp;tagMSG;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;Handled:&nbsp;Boolean);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;WndProc(var&nbsp;Message:&nbsp;TMessage);&nbsp;override;<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>var<br>&nbsp;&nbsp;Form1:&nbsp;TForm1;<br>&nbsp;&nbsp;MyMsgID:&nbsp;Cardinal;<br><br>implementation<br><br>{$R&nbsp;*.dfm}<br><br>procedure&nbsp;TForm1.FormCreate(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;//注册消息<br>&nbsp;&nbsp;MyMsgID&nbsp;:=&nbsp;RegisterWindowMessage('MyMessage');<br>end;<br><br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;//广播发送消息(实际使用时发送消息放在线程中)<br>&nbsp;&nbsp;PostMessage(HWND_BROADCAST,&nbsp;MyMsgID,&nbsp;0,&nbsp;0);//通过PostMessage发送时,窗体WndProc接收不到消息<br>&nbsp;&nbsp;//SendMessage(HWND_BROADCAST,&nbsp;MyMsgID,&nbsp;0,&nbsp;0);//通过SendMessage发送时,可以收到,但要等待消息处理完后才返回<br>&nbsp;&nbsp;ShowMessage('发送完成!');<br>end;<br><br>procedure&nbsp;TForm1.WndProc(var&nbsp;Message:&nbsp;TMessage);<br>begin<br>&nbsp;&nbsp;inherited;<br>&nbsp;&nbsp;if&nbsp;Message.Msg&nbsp;=&nbsp;MyMsgID&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Caption&nbsp;:=&nbsp;'收到消息01';<br>&nbsp;&nbsp;&nbsp;&nbsp;Sleep(1000);<br>&nbsp;&nbsp;end;<br>end;<br><br>procedure&nbsp;TForm1.ApplicationEvents1Message(var&nbsp;Msg:&nbsp;tagMSG;<br>&nbsp;&nbsp;var&nbsp;Handled:&nbsp;Boolean);<br>begin<br>&nbsp;&nbsp;if&nbsp;Msg.message&nbsp;=&nbsp;MyMsgID&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Caption&nbsp;:=&nbsp;'收到消息02';<br>&nbsp;&nbsp;&nbsp;&nbsp;Sleep(1000);<br>&nbsp;&nbsp;end;<br>end;<br><br>end.
 
HWND_BROADCAST是向顶层窗口发消息<br>DELPHI生成的应用程序中,顶层窗口是Application对象,一个隐藏的窗口,而你的Form1不是,所以收不到消息
 
&gt;&gt;不要告诉我循环所有窗口一个个发,因为我不知道有多不个窗口打开着!<br><br>screen.forms集合里有所有打开的窗口
 
有什么方法让所有窗口都收到消息呢?<br>本来SendMessage是可以的,但会造成阻塞
 
向screen.forms的每个窗口都发消息,整个程序效率可能会授影响,
 
PostMessage不会影响效率,但是会丢失消息。
 
当有一个MessageBox弹出时,收不到PostMessage发出的信息,怎么办
 
Application收到消息,不会自己去处理各个窗口,何必将消息发来发去,见了面还要打手机?&nbsp;一般情况下&nbsp;SendMessage&nbsp;跟调用函数或者过程没什么分别,如果不对外的话,不如<br>直接写调用函数,省得浪费系统资源,PostMessage&nbsp;则有延迟执行和合并执行的好处,用<br>的比较多。
 
因为窗口比较多,为了避免各个模块之间互相引用,引起模块划分混乱,才想用消息来传递系统状态的变化,使各个窗口自动更新状态!
 
后退
顶部