Board index » delphi » Adding Message Handlers with RegisterWindowMessage

Adding Message Handlers with RegisterWindowMessage

I have been trying unsuccessfully to get a standard Delphi message
handler to work with non-constant user-defined messages. What I want to
do is get a unique message ID using RegisterWindowMessage and use the
Delphi-style message handling like so:

type
  TForm1 = class(TForm)
  {...}
  protected
    procedure WMSignalClosed(var Message: TMessage); message WM_SIGNALCLOSED;
    {...}
  end;

var
  WM_SIGNALCLOSED : word;
  {...}

procedure TForm1.FormCreate(Sender: TObject);
begin
  WM_SIGNALCLOSED := RegisterWindowMessage('WM_SIGNALCLOSED');
  {...}
end;

procedure TForm1.WMSignalClosed(var Message: TMessage);
begin
  MessageDlg('Message received!', mtInformation, [mbOK], 0);
end;

The problem is that Delphi only wants to do this for pre-defined or
static messages. I want to make sure that I don't end up accidentally
receiving messages from another app that might be using the same message
ID as I am. This is a mission-critical application, so we cannot take ANY
chances on this happening. The only way that I can think of to do this
right now is to put it inside of a loop and keep on checking PeekMessage
to see if it occurs and then call the appropriate code from there, but it
seems like such an un-clean way of doing it. Seems like it would be
wasting a bit in the way of CPU time as well, but I haven't checked it
out yet.

Any ideas??? My sanity is in your hands.

Jim Kirk
International Pacific Trading Company

-------------------------------------------------------------------------------
jkirk                                     __  __     ____  ___       ___ ____
jk...@primenet.com                       /__)/__) / / / / /_  /\  / /_    /
                                        /   / \  / / / / /__ /  \/ /___  /
-------------------------------------------------------------------------------

 

Re:Adding Message Handlers with RegisterWindowMessage


On 11 Feb 1996 02:08:02 -0700, Jim Kirk <jk...@primenet.com> wrote:

Quote
>I have been trying unsuccessfully to get a standard Delphi message
>handler to work with non-constant user-defined messages. What I want to
>do is get a unique message ID using RegisterWindowMessage and use the
>Delphi-style message handling like so:

>type
>  TForm1 = class(TForm)
>  {...}
>  protected
>    procedure WMSignalClosed(var Message: TMessage); message WM_SIGNALCLOSED;
>    {...}
>  end;

>var
>  WM_SIGNALCLOSED : word;
>  {...}

Sorry, but you cannot do this. You must handle the message by
overriding WndProc and checking the message number explicitly.
Remember to call the inherited WndProc, to handle all other messages.
See the on-line documentation for more details.

procedure TForm1.WndProc(var Msg: TMessage);
begin
  if Msg.Message = WM_SignalClosed then
    ...
 else
  inherited WndProc(Msg);
end;

--
Ray Lischner         (li...@tempest-sw.com)
Tempest Software, Corvallis, Oregon, USA

Re:Adding Message Handlers with RegisterWindowMessage


        Alas you can't add a message handler unless you know the message number
at compile time, which you don't if you're using RegisterWindowsMessage like
ou should. But you can use TApplication.OnMessage. (Look up OnMessage or find it
under TApplication - there's an example and various SeeAlso's.)

--
David Ullrich
Don't you guys find it tedious typing the same thing
after your signature each time you post something?
I know I do, but when in Rome...

Re:Adding Message Handlers with RegisterWindowMessage


Hi Jim,

Perhaps this is what your after. The following code is straight out of a small app I wrote and is
used in conjunction with another Delphi apps.

Basically it registers the window message and to talk to the other app does a broadcast to all
windows messages. This means that you don't need the handle to the other window.

Then, in

 sendMessage(HWND_BROADCAST,value,form1.handle,longInt(pchar(gPc)));  {send message with pointer}

VALUE is the 'registerWindowMessage' word so the other app knows what to post back. I also send
this apps 'form' handle. This means that instead of doing a Hwnd_broadcast to all windows from
the other app, I can do it just to this window that sent the message. The last msg.lParam passes
a pointer to a global PCHAR memory location. I can use this pointer in the other app to access
memory (ie. info) which is alloacted in this app.

Finally, the other app posts a message back to this window which looks for the 'unique' register
windows message and has a msg.wParam of 1. If this condition exists then 'do something'. I close
the app.

Hope that makes sense and helps!

Here's the code....

Cheers,
Andrew McLean
Sydney, Australia
http://www.aerosoft.com.au
http://www.aerosoft.com.au/hitcount
http://www.aerosoft.com.au/delphi
http://www.aerosoft.com.au/delphi/progman

unit Hit001;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages,
  Forms;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    value: longInt;   {unique 'register windows message' value}
    gPc: PChar;
    { Private declarations }
  public
    { Public declarations }
    procedure WndProc(var msg: TMessage); override;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.WndProc(var msg: TMessage);
begin
  if (msg.msg=value) and (msg.wParam=1) then
  begin
    freeMem(gPc,255);
    close;   {other app posted the 'registerWindowsMessage' value and a '1'}
  end;

  inherited WndProc(msg);       { dispatch normally }
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  value:=registerWindowMessage('cgiWinHitCount');

  getMem(gPc,255);
  strPcopy(gPc,paramstr(1));
  sendMessage(HWND_BROADCAST,value,form1.handle,longInt(pchar(gPc)));  {send message with memory
pointer}
end;

end.

Other Threads