Link to home
Start Free TrialLog in
Avatar of qwertyuiopasdfghjkl
qwertyuiopasdfghjkl

asked on

Sending Message ?

following is my simple event, but it's just work in single unit, how to place the same message event to any forms i want. Once sending the message, all the forms with that event will be fire. it's something like broadcast the message.please help me with a sample code.

const
SX_MESSAGE = WM_USER;
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  public
  procedure MYEVENT(var Msg : TMessage);message SX_MESSAGE;
end;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
SendMessage(self.Handle,SX_MESSAGE,0,0);
end;

procedure TForm1.MYEVENT(var Msg: TMessage);
begin
showmessage('hello');
end;


ASKER CERTIFIED SOLUTION
Avatar of rwilson032697
rwilson032697

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Madshi
Madshi

Hi Raymond,

(1) Why not using Screen.Forms[i].Perform(SX_MESSAGE,0,0)?

(2) I'm not sure if he wants to know how to perform a broadcast message. However, it's just a part of the question. I think he wants to know how to easily add the event HANDLER to all the forms. Perhaps he should use a little component, what do you think?

qwert..., for what do you need that?

Regards, Madshi.
Ok...

Madshi: Yes .perform would also work.

qwert...: You can add the event to a base form class and then make sure all your forms are derived from that one. In this case you would type  the following code into a unit (don't create a form though!). Then when you create a form with New Form... change the declaration in the code so that the form is derived from TMessageForm instead of TForm (and then Robert is your fathers' brother!)

const
    SX_MESSAGE = WM_USER;
type
      TMessageForm = class(TForm)
      public
      procedure MYEVENT(var Msg : TMessage);message SX_MESSAGE;
    end;

Procedure TMessageForm.MYEVENT(var Msg : TMessage);

begin
// Do your stuff here
end;

Cheers,

Raymond.