function createform(cform:tform):boolean; //调用判断是否创建窗件函数 var NO:integer; formexist:boolean; begin formexist:=false; if cform=NIl then begin createform:=false; exit; end; for NO:=0 to screen.FormCount -1 do begin if screen.Forms[NO].ClassType =cform.ClassType then formexist:=true; end; if formexist=false then begin createform:=false; exit; end; if cform.WindowState =wsminimized then showwindow(cform.Handle ,sw_shownormal) else showwindow(cform.Handle ,sw_showNA); if not cform.Visible then cform.Visible :=true; cform.BringToFront ; cform.SetFocus ; createform:=true; end;
在主窗体里用以下代码调用:
if createform(f_dept)=false then //判断是否创建,有就显示 f_dept:=tf_dept.Create(application); //没有则创建
看看偶自己用的函数: function FindFormFromScreen(const sFormName: String): TCustomForm; var //通过名称查找窗体 I: Integer; begin Result := nil; for I := 0 to Screen.CustomFormCount-1 do if Screen.CustomForms[I].Name = sFormName then begin Result := Screen.CustomForms[I]; Break; end; end;
function CreateForm(const sFormName: String; AFormClass: TCustomFormClass; var AForm: TCustomForm; bShow: Boolean): Boolean; begin //创建窗体 MDIChild 不需要 AForm.Show, 即 bShow 应 = False. Result := False; AForm := FindFormFromScreen(sFormName); if AForm = nil then begin AForm := AFormClass.Create(Application); Result := True; end; if bShow and Assigned(AForm) then AForm.Show; end;