Einzelnen Beitrag anzeigen

Kas Ob.

Registriert seit: 3. Sep 2023
379 Beiträge
 
#9

AW: Objekte in verschiedenen Formularen zentral initialisieren

  Alt 12. Jan 2025, 08:44
Well, if all your forms are already dpr created, meaning they are in the available forms in project options, then something like this should work
Delphi-Quellcode:
procedure TMainForm.FormCreate(Sender: TObject);
begin
  // These also could be adjusted and moved to initialization section
  FAlreadyActivated := False;

  // in case OnActivate is used in the project then it should be stored and restored later*
  Application.OnActivate := OnApplicationActivate;
end;

procedure TMainForm.OnApplicationActivate(Sender: TObject);
var
  C: Integer;
begin
  // ExitProc assigned in TApplication.Run meanning it could be used as indicator for done creating forms
  // can help in case Application.Activate wrongly (missused by) being called from a form constructor
  if not Assigned(ExitProc) then
    Exit;

  if not FAlreadyActivated then
  begin
    C := Application.ComponentCount;
    while C > 0 do
    begin
      Dec(C);
      if Application.Components[C] is TForm then
        // InitFormControls(Application.Components[C]);

    end;
  end;
  FAlreadyActivated := True;
  // here is later* ,( to restore original or more useful OnActivate)
end;
For runtime created forms and to minimize and centralize the code, use DDetours to hook TForm.Create then execute the original code followed by your InitFormControls after checking that the TForm is one of yours, if you would use DDetours then you don't any any code like the above.

https://github.com/MahdiSafsafi/DDetours
Kas
  Mit Zitat antworten Zitat