Delphi 如何使用泛型创建不同类型的MDI子级?
我需要将MDI子表单的创建集中到Delphi(VCL)中的一个独特过程中。其思想是在每次创建MDI子窗体时执行一些操作,无论其类型如何,即将其标题名称添加到列表中以访问该MDI子窗体。像这样:Delphi 如何使用泛型创建不同类型的MDI子级?,delphi,generics,vcl,mdi,Delphi,Generics,Vcl,Mdi,我需要将MDI子表单的创建集中到Delphi(VCL)中的一个独特过程中。其思想是在每次创建MDI子窗体时执行一些操作,无论其类型如何,即将其标题名称添加到列表中以访问该MDI子窗体。像这样: procedure TMainForm<T>.CreateMDIChild(const ACaption : String); var Child: T; begin { create a new MDI child window }
procedure TMainForm<T>.CreateMDIChild(const ACaption : String);
var
Child: T;
begin
{ create a new MDI child window }
Child := T.Create(Application);
Child.Caption := ACaption;
// add this child to the list of active MDI windows
...
end;
procedure TMainForm.Button1Click(Sender : TObject);
begin
CreateMDIChild<TMdiChild1>('Child type 1');
CreateMDIChild<TMdiChild2>('Child type 2');
...
procedure tmaninform.CreateMDIChild(const acoption:String);
变量
儿童:T;
开始
{创建新的MDI子窗口}
子项:=T.Create(应用程序);
Child.Caption:=acoption;
//将此子项添加到活动MDI窗口列表中
...
结束;
程序TMainForm.按钮1单击(发件人:ToObject);
开始
CreateMDIChild(“子类型1”);
CreateMDIChild(“子类型2”);
...
但是,我没有使用泛型的经验。任何帮助我都会感激的。
非常感谢。您可以使用unit
System.Generics.Collections
中的类。例如,为了解决类似的任务,我使用了TObjectList
,其中TfmMDIChild
我自己的类。
另一个有用的技巧是,您可以根据TObjectList
创建自己的类来保存集合。
我做了这样的东西:
TWindowList = class(TObjectList<TfmMDIChild>)
public
procedure RefreshGrids;
function FindWindow(const AClassName: string; AObjCode: Integer = 0): TfmMDIChild;
procedure RefreshWindow(const AClassName: string; AObjForRefresh: integer = 0; AObjCode: Integer = 0);
procedure RefreshToolBars;
end;
TGenericMDIForm <T:TForm> = class
class procedure CreateMDIChild(const Name: string);
end;
TWindowList=class(TObjectList)
公众的
程序更新网格;
函数FindWindow(const AClassName:string;AObjCode:Integer=0):TfmMDIChild;
过程刷新窗口(const AClassName:string;AObjForRefresh:integer=0;AObjCode:integer=0);
程序刷新工具栏;
结束;
您可以定义一个类来创建表单(使用泛型),该类具有如下类约束:
TWindowList = class(TObjectList<TfmMDIChild>)
public
procedure RefreshGrids;
function FindWindow(const AClassName: string; AObjCode: Integer = 0): TfmMDIChild;
procedure RefreshWindow(const AClassName: string; AObjForRefresh: integer = 0; AObjCode: Integer = 0);
procedure RefreshToolBars;
end;
TGenericMDIForm <T:TForm> = class
class procedure CreateMDIChild(const Name: string);
end;
TGenericMDIForm=class
类过程CreateMDIChild(const Name:string);
结束;
通过这一实施:
class procedure TGenericMDIForm<T>.CreateMDIChild(const Name: string);
var
Child:TCustomForm;
begin
Child := T.Create(Application);
Child.Caption := Name + ' of ' + T.ClassName + ' class';
end;
类过程TGenericMDIForm.CreateMDIChild(const Name:string);
变量
儿童:TCustomForm;
开始
子项:=T.Create(应用程序);
Child.Caption:=Name+'of'+T.ClassName+'class';
结束;
现在,您可以使用它创建MDIChil表单不同的类:
procedure TMainForm.Button1Click(Sender: TObject);
begin
TGenericMDIForm<TMdiChild>.CreateMDIChild('Child type 1');
TGenericMDIForm<TMdiChild2>.CreateMDIChild('Child type 2');
end;
程序TMA通知。按钮1点击(发送者:ToObject);
开始
TGenericMDIForm.CreateMDIChild('Child type 1');
TGenericMDIForm.CreateMDIChild('Child type 2');
结束;
将
类约束
与泛型TGenericMDIForm=class
一起使用,可以避免有人尝试使用类似的TGenericMDIForm.CreateMDIChild('Child type 1')代码>与一个不是t表单
后代的类一起使用。我想您已经阅读了?您不能在表单类中使用泛型。@DelphiCoder是的,您可以是的,我阅读了文档,但我做错了什么=(这是我第一次尝试使用泛型,所以我没有这种方法的经验。好吧,安东,你的解决方案非常有趣,我是泛型新手,我认为这种方法在未来会非常有用。提前谢谢=)