无法实现
V_FORMNAME 是字符串,不是类
要想实现类似功能只能是判断
如:
// V_FORMNAME := 'TForm1' // TForm1是字符
if V_FORMNAME = 'TForm1' then
begin
with V_FORMNAME.Create(Application) do
begin
ShowModal;
Free;
end;
end
else if V_FORMNAME = 'TForm2' then
begin
with V_FORMNAME.Create(Application) do
begin
ShowModal;
Free;
end;
end;
V_FORMNAME 是字符串,不是类
要想实现类似功能只能是判断
如:
// V_FORMNAME := 'TForm1' // TForm1是字符
if V_FORMNAME = 'TForm1' then
begin
with V_FORMNAME.Create(Application) do
begin
ShowModal;
Free;
end;
end
else if V_FORMNAME = 'TForm2' then
begin
with V_FORMNAME.Create(Application) do
begin
ShowModal;
Free;
end;
end;
V_FORMNAME 是字符串,不是类
要想实现类似功能只能是判断
如:
// V_FORMNAME := 'TForm1' // TForm1是字符
if V_FORMNAME = 'TForm1' then
begin
with V_FORMNAME.Create(Application) do
begin
ShowModal;
Free;
end;
end
else if V_FORMNAME = 'TForm2' then
begin
with V_FORMNAME.Create(Application) do
begin
ShowModal;
Free;
end;
end;
begin
with FClass.Create(self) do
try
ShowModal;
finally
Free;
end;
end;procedure TFMain.Button1Click(Sender: TObject);
begin
CreateForm(TForm2);
end;
type
TFormClass=class of TMDIChild;
implementation
procedure TMainForm.ShowForm(const FormClass: String);
var
i:integer;
HasCreated:boolean;
FormName:String;
Child:TMDIChild;
ChildClass:TFormClass;
begin
Child:=nil;
HasCreated:=false;
//獲取子窗體的類別
ChildClass:=TFormClass(GetClass(FormClass));
if ChildClass=nil then
begin
ShowMessage('該類未注冊﹗請到主窗體的initialization中注冊相應的類別﹗');
Exit;
end;
//獲取子窗體的名稱
FormName:=Copy(FormClass,1,Length(FormClass)-1);
//檢查子窗體是否已經創建
for i := 0 to MDIChildCount-1 do
begin
if MDIChildren[i].Name=FormName then
begin
HasCreated:=true;
Child:=TMDIChild(MDIChildren[i]);
break;
end;
end;
//如果沒有創建窗體則創建
if HasCreated=false then
begin
Child:=ChildClass.Create(Application);
Child.Name:=FormName;
Child.Show;
end
else
Child.BringToFront;
end;
initialization
RegisterClasses([TForm1]);
end.