Send a custom message to all forms in application

Quick tutorial on how to send a custom windows message to all forms in an application

This sounds like it should be a fairly simple task. Unfortunately the documentation in delphi is lacking on this subject.
To start this tutorial create a new project in delphi and add 2 new forms. Save all files and use the default names of Form1, Form2, Form3.

We are now ready :)

There are a few easy steps to follow.

  • Define Message Constants
  • Define message handlers
  • Send message to all forms

Define Message Constants
This is easy, just add the declaration in the uses section of form1 as follows:

  JR_1 = WM_USER + 1;

WM_USER is the windows defined area of WM values that you are allowed to use. When creating custom messages always start at WM_USER + X..., values to avoid are WM_USER + 100, 500, 1000, etc. because component writers tend to use these common values for their own purposes.

Define message handlers

To make this clearer and reduce code i have created a form class that all forms in this example will be based on:

Add this code to the Uses section beneath the const declaration just added.

  TMessageForm = class (TForm)
    procedure JR1(var Message: TMessage); message JR_1;

Now add the following code to the implementation section.

{- TMessageForm.JR1 }
procedure TMessageForm.JR1(var Message: TMessage);
  Caption := 'Message Recieved';

Ok, so we now have a form class that can respond to our custom message. All we need to do is change all the forms to be derived from TMessageForm instead of TForm, like so:

TForm1 = class (TMessageForm)

You will need to add 'unit1' to your uses clause in units 2 & 3 like so.

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Unit1;

You will also need to make all the forms visible at start up. we do this by editing the projects .dpr file. This can be done by opening in the file in notepad, or by using the Project Manager.

To open the Project Manager press CTRL+ALT+F11, then select right click the project name and select 'view source'

Paste the following code:

  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2},
  Unit3 in 'Unit3.pas' {Form3};

{$R *.RES}

  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  ShowWindow(Form2.Handle, SW_SHOW);
  Application.CreateForm(TForm3, Form3);
  ShowWindow(Form3.Handle, SW_SHOW);

We added Windows to the uses section because we need to call ShowWindow. ShowWindow, does exactly what it says on the tin ;) it shows a form by its handle.

Now we are ready to send the messages to all the forms in the application.

Send message to all forms

Add a button to the form1 and dbl click it.
Paste this code.

{- TForm1.Button1Click }
procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
  for i:=0 to Screen.FormCount -1 do

Now run the application and your all sorted ;)
If you have trouble following this article then you can download the full source code here:


Share this article!

Follow us!

Find more helpful articles: