How to make a child form listen to broadcasted messages #89
Question
I have two applications that I wish to talk with each other via Windows
messages. Due to the nature of these applications, it would work best if
I post a message using:
PostMessage(HWND_BROADCAST, MyMsg, 0, 0);
My question is how does a child form listen to messages that have been
broadcasted without interfearing with them. (Some how make a hook into
Application.OnMessage) but I'm not sure how to safely hook in
and back out.
In the sender add a procedure such as:
procedure TForm1.Button2Click(Sender: TObject); var Receiver: THandle; buffer: array[0..1023] of char; Data: TCopyDataStruct; begin Receiver := FindWindow('TReceiverForm', nil); if Receiver > 0 then begin StrPLCopy(@buffer, Edit1.Text, 1024); // Assumes message in Editbox Data.dwData := 0; Data.cbData := 1024; Data.lpData := @buffer; SendMessage(Receiver, WM_COPYDATA, 0, LongInt(@Data)); end; end;
In the receiver add these:
type TForm1 = class(TForm) { ... } private { ... } protected procedure WmCopyData(var Msg: TMessage); message WM_COPYDATA; public { ... } end; procedure TForm1.WmCopyData(var Msg: TMessage); begin ShowMessage( 'This message sent:' + #13 + string(PChar(PCopyDataStruct(Msg.LParam)^.lpData)) ); end;
Original resource: | The Delphi Pool |
---|---|
Author: | Charles Hacker |
Added: | 2009/08/24 |
Last updated: | 2009/08/24 |