Home > Blockchain >  Get notified when a new form is shown/open
Get notified when a new form is shown/open

Time:03-16

TApplication triggers the event OnModalBegin when a modal TForm is opened.

Is there a way to get notified when a non modal TForm is shown/opened the same wayt TApplication.OnModalBegin does ?

CodePudding user response:

You can capture some messages on TApplicationEvents that can help you to detect when a new form is created/showed.

Use this code on OnMessage event of TApplicationEvents component.

procedure TFormMain.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
var
  f:TCustomForm;

  function GetFormByHandle(const AHandle:Hwnd):TCustomForm;
  var
    i:Integer;
  begin
    Result := nil;
    for i := 0 to (Screen.FormCount - 1) do
      if (Screen.Forms[i].Handle = AHandle) then
        Result := Screen.Forms[i];
  end;
begin
  if (Msg.message = WM_DWMNCRENDERINGCHANGED) then begin  // detect new form
    f := GetFormByHandle(Msg.hwnd);  // Search on Scren by handle
    if Assigned(f) then
      Memo1.Lines.Add('   Name:'   f.Name   '   - Handle: '   IntToStr(Msg.hwnd)   '  - Classname: '   f.ClassName);  /7 show info
  end;
end;

When a new form is created you can obtain his information using the Handle (come with params of message) and interrogate the Screen object (singleton created).

enter image description here

  • Related