我需要在Delphi (VCL)中把MDI子表格的创建集中到一个独特的过程中。我们的想法是每次创建MDI子表格时,无论其类型如何,都要做一些操作,例如,将其标题名称添加到列表中,以获得对该MDI子表格的访问。像这样。
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');
...
但是,我对属相没有经验。任何帮助我都会感激不尽.非常感谢你。
你可以用这样的类约束来定义一个类来创建通用的表单(使用通用)。
TGenericMDIForm <T:TForm> = class
class procedure CreateMDIChild(const Name: string);
end;
并且用这个实现。
class procedure TGenericMDIForm<T>.CreateMDIChild(const Name: string);
var
Child:TCustomForm;
begin
Child := T.Create(Application);
Child.Caption := Name + ' of ' + T.ClassName + ' class';
end;
现在,你可以用它来创建MDIChil表格的不同类。
procedure TMainForm.Button1Click(Sender: TObject);
begin
TGenericMDIForm<TMdiChild>.CreateMDIChild('Child type 1');
TGenericMDIForm<TMdiChild2>.CreateMDIChild('Child type 2');
end;
使用 class constraint
与一般 TGenericMDIForm <T:TForm> = class
,你可以避免别人尝试使用这样的东西。TGenericMDIForm<TMemo>.CreateMDIChild('Child type 1');
与一个不属于 TForm
子孙。
你可以使用单元中的类 System.Generics.Collections
. 例如,在解决类似的任务时,我使用的是 TObjectList<TfmMDIChild>
,其中 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;