Avatar of rainwise
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_MESSAGE,0,0); // this does work
Delphi

Avatar of undefined
Last Comment
rainwise
Avatar of mokule
mokule
Flag of Poland image

> self.Broadcast(Msg); // This doesn't work

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
Avatar of rainwise
rainwise

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;
ASKER CERTIFIED SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of rainwise
rainwise

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
Delphi
Delphi

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.

60K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo