How to make a child form listen to broadcasted messages
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;