rainwise
asked on
Trouble with broadcasting messages (TWinControl)
{ Delphi 7 on XP Pro}
Hi,
I am trying to send a custom broadcast message to all components within my application. I can send a custom message using "PostMessage" from within the main form and the forms handler gets it fine. If I try a broadcast the handler doesn't catch it. The Delphi help shows how to send the broadcast message but not how to trap them. Could someone provide a code snippet to illustrate how to post and catch a custom broadcast message. Here are some of the key functions from my test program:
const
MY_MESSAGE= WM_USER +5;
private { Form1 }
procedure MessageHandler(var M: TMessage); message MY_MESSAGE;
{ The message handler for the form }
procedure TForm1.MessageHandler(var M: TMessage);
begin ......
procedure TForm1.FormCreate(Sender: TObject);
var
Msg:TMessage;
begin
Msg.Msg := MY_MESSAGE;
Msg.WParam := 0;
Msg.LParam := Longint(Self);
Msg.Result := 0;
self.Broadcast(Msg); // This doesn't work
// PostMessage(handle,MY_MESS AGE,0,0); // this does work
Hi,
I am trying to send a custom broadcast message to all components within my application. I can send a custom message using "PostMessage" from within the main form and the forms handler gets it fine. If I try a broadcast the handler doesn't catch it. The Delphi help shows how to send the broadcast message but not how to trap them. Could someone provide a code snippet to illustrate how to post and catch a custom broadcast message. Here are some of the key functions from my test program:
const
MY_MESSAGE= WM_USER +5;
private { Form1 }
procedure MessageHandler(var M: TMessage); message MY_MESSAGE;
{ The message handler for the form }
procedure TForm1.MessageHandler(var M: TMessage);
begin ......
procedure TForm1.FormCreate(Sender: TObject);
var
Msg:TMessage;
begin
Msg.Msg := MY_MESSAGE;
Msg.WParam := 0;
Msg.LParam := Longint(Self);
Msg.Result := 0;
self.Broadcast(Msg); // This doesn't work
// PostMessage(handle,MY_MESS
ASKER
Ok, I can see that but I am still not able to get it to work with a custom TComponent. The components "MessageHandler" never gets called. Sorry, I am probably missing something really simple.
type
TDataSet = class (TComponent)
private
val: integer;
procedure SetVal(n: integer);
procedure MessageHandler(var M: TMessage); message MY_MESSAGE;
public
constructor Create( AOwner: TComponent);override;
destructor Destroy; override;
published
property value: integer read val write SetVal;
end;
//------------------------ ---
procedure TForm1.FormCreate(Sender: TObject);
begin
Msg.Msg := MY_MESSAGE;
Msg.WParam := 0;
Msg.LParam := Longint(Self);
Msg.Result := 0;
MyDataSet:=TDataSet.Create (self); // the component is created here
end;
type
TDataSet = class (TComponent)
private
val: integer;
procedure SetVal(n: integer);
procedure MessageHandler(var M: TMessage); message MY_MESSAGE;
public
constructor Create( AOwner: TComponent);override;
destructor Destroy; override;
published
property value: integer read val write SetVal;
end;
//------------------------
procedure TForm1.FormCreate(Sender: TObject);
begin
Msg.Msg := MY_MESSAGE;
Msg.WParam := 0;
Msg.LParam := Longint(Self);
Msg.Result := 0;
MyDataSet:=TDataSet.Create
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks mokule,
Yes, your example works well. It seems that setting the Parent property to self is the key. I was able to get my code working by making my class a descendent of TWinControl and setting the parent property.
All the references I have don't go into detail about exactly what is required to receive broadcast messages. Your example was just what I needed to get back on track. Thanks
Yes, your example works well. It seems that setting the Parent property to self is the key. I was able to get my code working by making my class a descendent of TWinControl and setting the parent property.
All the references I have don't go into detail about exactly what is required to receive broadcast messages. Your example was just what I needed to get back on track. Thanks
From delphi help
Sends a message to each of the CHILD controls.
It sends only to child.
And You are trying to receive it not in child