将继承的帧作为参数传递给过程

Passing inherited frames as argument to a procedure

我的主窗体中有一个 TPageControl 和 N 个 TTabSheets,我用它来嵌入几个 TFrame 后代。 对于我创建的框架 "TBaseFrame",我从中派生出要在 TabSheets 中显示的各个框架,或多或少看起来像这样...

TBaseFrame = class(TFrame)

我正在努力解决的问题是:我想创建一个过程,将我的任何 TBaseFrameDescendants 作为参数,创建给定的框架并将其显示在新选项卡中sheet。我从这样的事情开始......

procedure CreateNewTabSheetAndFrame( What do I put here to accept any of my TBaseFrameDescendants? )
var
  TabSheet: TTabSheet;

begin
  TabSheet := TTabSheet.Create(MainPageControl);
  TabSheet.Caption := 'abc';
  TabSheet.PageControl := MainPageControl;

// Here I want to create the given TBaseFrameDescendant, set the Parent to the above TabSheet and so on   
end;

我想我的主要问题是如何设置我的程序,以便我可以传入从我的 TBaseFrame 派生的任何框架,以便我可以在程序中使用它,或者我在这里走错了方向?

您需要使用所谓的元类。

type
  TBaseFrameClass = class of TBaseFrame;

procedure TMainForm.CreateNewTabSheetAndFrame(FrameClass: TBaseFrameClass)
var
  TabSheet: TTabSheet;
  Frame: TBaseFrame;
begin
  TabSheet := TTabSheet.Create(Self);
  TabSheet.PageControl := MainPageControl;
  Frame := FrameClass.Create(Self);
  Frame.Parent := TabSheet;
end;

如果您在任何框架 类 中声明任何构造函数,请确保它们派生自 TComponent 中引入的虚拟构造函数。这是必要的,以便通过元类进行实例化以调用适当的派生构造函数。