RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TApplication.OnMessage Event

Occurs when the application receives a Windows message.

Pascal
property OnMessage: TMessageEvent;
C++
__property TMessageEvent OnMessage;

Use OnMessage to trap any or all Windows messages posted to all windows in the application. The OnMessage event occurs when an application receives a Windows message. An OnMessage event handler allows an application to respond to messages other than those declared in the events for TApplication. If the application doesn't have a specific handler for an incoming message, the message is dispatched to the window for which it was intended, and Windows handles the message.

Note: OnMessage only receives messages that are posted to the message queue, not those sent directly with the Windows API SendMessage function.
Warning: Caution:Thousands of messages per second flow though this event. Be careful when coding the handler, because it can affect the performance of the entire application.
Note: You can also respond to this event using the TApplicationEvents component, which allows you to assign an event handler using the IDE.
 

C++ Examples: 

 

/*
The following code handles a custom message that the
application sends to itself when a file is ready for reading.
*/
const WM_FILEREADY = WM_USER + 2000;

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  const PChar Path = "../overview.rtf";
  PostMessage(Application->Handle, WM_FILEREADY, 0, int(Path));
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Application->OnMessage = AppMessage;
}

void __fastcall TForm1::AppMessage(tagMSG &Msg, bool &Handled)
{
  if (Msg.message == WM_FILEREADY)
  {
    Memo1->Lines->LoadFromFile(AnsiString((char *)Msg.lParam));
    Handled = true;
  }
/*
For all other messages, Handled remains False so that other
message handlers can respond.
*/
}

 

Delphi Examples: 

{
The following code handles a custom message that the
application sends to itself when a file is ready for reading.
}
const WM_FILEREADY = WM_USER + 2000;
procedure TForm1.Button1Click(Sender: TObject);
const Path: PChar = 'OverView.RTF';
begin
  PostMessage(Application.Handle, WM_FILEREADY, 0, integer(Path));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnMessage := AppMessage;
end;

procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
var msgStr: String;
begin
  if Msg.hwnd = Application.Handle then
  begin
    if Msg.message = WM_FILEREADY then
    begin
      msgStr:= PChar(Msg.lParam);
      Memo1.Lines.LoadFromFile(msgStr);
      Handled := True;
    end;
  end;
  { for all other messages, Handled remains False }
  { so that other message handlers can respond }
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!