参照一下 procedure OpenChildForm(FormClass: TFormClass; var Fm; AOwner:TComponent); var i: integer; Child:TForm; begin for i := 0 to Screen.FormCount -1 do if Screen.Forms[i].ClassType=FormClass then begin Child:=Screen.Forms[i]; if Child.WindowState=wsMinimized then ShowWindow(Child.handle,SW_SHOWNORMAL) else ShowWindow(Child.handle,SW_SHOWNA); if (not Child.Visible) then Child.Visible:=True; Child.BringToFront; Child.Setfocus; TForm(Fm):=Child; exit; end; Child:=TForm(FormClass.NewInstance); TForm(fm):=Child; Child.Create(AOwner); end;procedure TMainForm.Button1Click(Sender: TObject); begin OpenChildForm(TChildForm1,ChildForm1, Self); end;
procedure Tform1.buttonclick(sender: TObject); var form2: Tform2; begin if assigned(form2) then form2:=Tform2.create(self); form2.show; end;procedure Tform2.formclose(Sender: TObject; var Action:TCloseAction);
procedure OpenChildForm(FormClass: TFormClass; var Fm; AOwner:TComponent);
var
i: integer;
Child:TForm;
begin
for i := 0 to Screen.FormCount -1 do
if Screen.Forms[i].ClassType=FormClass then
begin
Child:=Screen.Forms[i];
if Child.WindowState=wsMinimized then
ShowWindow(Child.handle,SW_SHOWNORMAL)
else
ShowWindow(Child.handle,SW_SHOWNA);
if (not Child.Visible) then Child.Visible:=True;
Child.BringToFront;
Child.Setfocus;
TForm(Fm):=Child;
exit;
end;
Child:=TForm(FormClass.NewInstance);
TForm(fm):=Child;
Child.Create(AOwner);
end;procedure TMainForm.Button1Click(Sender: TObject);
begin
OpenChildForm(TChildForm1,ChildForm1, Self);
end;
var
form2: Tform2;
begin
if assigned(form2) then
form2:=Tform2.create(self);
form2.show;
end;procedure Tform2.formclose(Sender: TObject; var Action:TCloseAction);
begin
form2:=nil;
action:=cafree;
end;
SmallMaker(小人物) 的代码量太大,不想看!
2.判断xxxForm是否为nil 如果是就创建不是就Show
注意在窗体关闭事件加入xxxForm:=nil