Home > Delphi > Handle messages without forms?

Handle messages without forms?

November 14, 2013 Leave a comment Go to comments

Want to handle messages without creating an actual form? Send messages between a thread and your main app? Or just impress your friends by using messages to communicate between forms? This is a very handy class that i’ve used over the years. Just override “HandleMessage” to intercept and process your own messages (ps: dispatch is your friend).

  TMessageHandler = Class(TObject)
  private
    FHandle:    HWND;
  protected
    Property    Handle:HWND read FHandle;
    Procedure   HandleMessage(var message:TMessage);virtual;
  public
    Constructor Create;virtual;
    Destructor  Destroy;Override;
  End;

constructor TMessageHandler.Create;
begin
  inherited Create;
  FHandle:=AllocateHWnd(HandleMessage);
end;

destructor TMessageHandler.Destroy;
begin
  DeallocateHWnd(FHandle);
  inherited;
end;

procedure TMessageHandler.HandleMessage(var Message:TMessage);
begin
  Message.Result := DefWindowProc(FHandle, Message.Msg,
  Message.wParam, Message.lParam);
end;

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: