OnMessage (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following code handles a custom message that the application sends to itself when a file is ready for reading.

Code

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;

Uses