对于您的第一个问题:添加另一个构造函数,例如 CreateAsMDI,如下所示:
constructor TModalAndMDIForm.CreateAsMDI(AOwner: TComponent);
begin
f_blChild := true;
GlobalNameSpace.BeginWrite;
try
inherited CreateNew(AOwner);
if(not(csDesigning in ComponentState)) then begin
Include(FFormState, fsCreating);
try
FormStyle := fsMDIChild;
if(not(InitInheritedComponent(self, TForm))) then
raise Exception.CreateFmt('Can't create %s as MDI child', [ClassName]);
finally
Exclude(FFormState, fsCreating);
end;
end;
finally
GlobalNameSpace.EndWrite;
end;
end;
在普通构造函数中,只需将变量 f_blChild 设置为 false 并调用继承的 create。
你还需要两件事,而不是自我解释:
procedure TModalAndMDIForm.Loaded;
begin
inherited;
if(f_blChild) then
Position := poDefault
else begin
Position := poOwnerFormCenter;
BorderStyle := bsDialog;
end;
end;
//-----------------------------------------------------------------------------
procedure TModalAndMDIForm.DoClose(var Action: TCloseAction);
begin
if(f_blChild) then
Action := caFree;
inherited DoClose(Action);
end;
如果使用标准构造函数创建,现在您可以调用窗体模式,如果使用 CreateAsMDI 创建,则可以调用 MDI 子窗体。
如果您在表格声明中包含此内容
property IsChild: boolean read f_blChild;
您甚至可以根据表单是否为 MDI 子级来执行操作,只需询问 isChild 属性即可。
至于您的第二个问题:不要使用 Application.CreateForm,而是自己创建表单:
这里是 modal 和 MDI 的两个创作:
//Modal
frmDialog := TMyForm.Create(self);
// Your Code
frmDialog.ShowModal;
frmDialog.Release;
//MDI-Child
frmDialog := TMyForm.CreateChild(self);
// Your code
frmDialog.Show;
我已将此答案翻译成DelphiPraxis网站上的一篇文章。