[fpc-pascal]Message methods - DefaultHandler

Thomas Schatzl tom_at_work at gmx.at
Tue Jun 8 17:28:22 CEST 2004


Hello,

>      I have some problem with DefaultHandler method. When I pass a
>message for which is special handler defined everything is ok, but when
>it should be passed to DefaultHandler (which is overriden) it does
>nothing. Here is some sample code:
>
> {$MODE OBJFPC}
> type TMyMessage = record
>        MSGID : cardinal;
>        text : string;
>        end;
>
>      TTest=class(TObject)
>        constructor Init;
>        procedure Handle (var MyMessage : TMyMessage); Message 1; virtual;
>        procedure DefaultHandler(var MyMessage : TMyMessage); virtual;
>        destructor Done;
>        end;
>
> It should write two times the "Hello world message". For the first time
from
> method TTest.Handle, and for the second time from method
> TTest.DefaultHandler. But TTest.DeafultHandler doesn't do anything
> (it writes only one message).

The DefaultHandler method has a wrong signature and so your method is not
called. You have to override

    procedure Dispatch(var Message); override;

and use a typecast in the method implementation, e.g.

procedure TTest.DefaultHandler(var MyMessage);
begin
  writeln(TMyMessage(MyMessage).Text);
end;

Note that Delphi (don't have FPC installed on this machine, sorry) complains
that message methods may not be declared as virtual (doesn't make much sense
imo, since dispatching a message to an object always is "bound" dynamically
using the message identifier).

Hth,
  Thomas





More information about the fpc-pascal mailing list