OnMessage (C++)

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

int WM_FILEREADY = WM_USER + 2000;

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

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  Application->OnMessage = AppMessage;
}

void __fastcall TForm1::AppMessage(tagMSG &Msg, bool &Handled)
{
  if (Msg.message == WM_FILEREADY)
  {
	Memo1->Lines->LoadFromFile(String((wchar_t *)Msg.lParam));
    Handled = true;
  }

  /*
    For all other messages, Handled remains False 
    so that other message handlers can respond.
  */
}

Uses