T
tonmy
Unregistered / Unconfirmed
GUEST, unregistred user!
下面程序通过PostMessage发送,窗体的WndProc收不到消息,Application.OnMessage则收到;用SendMessage发送时,则相反,但SendMessage发送会造成阻塞,有什么办法发一个消息,程序所有窗口都能收到,同时消息的处理不影响发送消息的线程;不要告诉我循环所有窗口一个个发,因为我不知道有多不个窗口打开着!<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, AppEvnts;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> ApplicationEvents1: TApplicationEvents;<br> procedure FormCreate(Sender: TObject);<br> procedure Button1Click(Sender: TObject);<br> procedure ApplicationEvents1Message(var Msg: tagMSG;<br> var Handled: Boolean);<br> private<br> { Private declarations }<br> procedure WndProc(var Message: TMessage); override;<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br> MyMsgID: Cardinal;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> //注册消息<br> MyMsgID := RegisterWindowMessage('MyMessage');<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> //广播发送消息(实际使用时发送消息放在线程中)<br> PostMessage(HWND_BROADCAST, MyMsgID, 0, 0);//通过PostMessage发送时,窗体WndProc接收不到消息<br> //SendMessage(HWND_BROADCAST, MyMsgID, 0, 0);//通过SendMessage发送时,可以收到,但要等待消息处理完后才返回<br> ShowMessage('发送完成!');<br>end;<br><br>procedure TForm1.WndProc(var Message: TMessage);<br>begin<br> inherited;<br> if Message.Msg = MyMsgID then<br> begin<br> Caption := '收到消息01';<br> Sleep(1000);<br> end;<br>end;<br><br>procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;<br> var Handled: Boolean);<br>begin<br> if Msg.message = MyMsgID then<br> begin<br> Caption := '收到消息02';<br> Sleep(1000);<br> end;<br>end;<br><br>end.