Delphi: Detect when a new form has been created

后端 未结 3 1166
暖寄归人
暖寄归人 2021-01-20 09:46

I\'d like to detect when a new form has been created.

Now I use the Screen.ActiveFormChange event and check for new forms in Screen.CustomForms

3条回答
  •  鱼传尺愫
    2021-01-20 10:26

    Track Screen.CustomFormCount in Application.OnIdle:

      private
        FPrevFormCount: Integer;
      end;
    
    procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
    begin
      if Screen.CustomFormCount > FPrevFormCount then
        Caption := Caption + ' +1';
      if Screen.CustomFormCount <> FPrevFormCount then
        FPrevFormCount := Screen.CustomFormCount;
    end;
    
    procedure TForm1.TestButton1Click(Sender: TObject);
    begin
      TForm2.Create(Self).Show;
    end;
    
    procedure TForm1.TestButton2Click(Sender: TObject);
    begin
      ShowMessage('Also trackable?');  // Yes!
    end;
    
    procedure TForm1.TestButton3Click(Sender: TObject);
    begin
      OpenDialog1.Execute; // Doesn't update Screen.CustomFormCount
    end;
    

    Native dialogs managed and shown by Windows (TOpenDialog, TFontDialog, etc...) are created apart from the VCL and to track them also, you need a hacking unit. Try this one then.

提交回复
热议问题