我的主窗体中有一个 TPageControl,其中包含 N 个 TTabSheets,我用它来嵌入多个 TFrame 后代。 对于我创建的“TBaseFrame”框架,我从中派生出要在 TabSheets 中显示的各个框架,或多或少看起来像这样......
TBaseFrame = 类(TFrame)
- TBaseFrameDescendant1 = 类(TBaseFrame)
- TBaseFrameDescendant2 = 类(TBaseFrame)
- TBaseFrameDescendantN = 类(TBaseFrame)
我正在努力解决的是:我想创建一个过程,它将我的任何 TBaseFrameDescendants 作为参数,创建给定的框架并将其显示在新的选项卡中。我从这样的事情开始......
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
中引入的虚拟构造函数。为了通过元类实例化调用适当的派生构造函数,这是必要的。
关于function - 将继承的帧作为参数传递给过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59561815/