我在写MDI时发现同一处子窗口能重复出现,怎么解决呀? 我写的函数如下,可就是解决不了,求助 function OpenForm(WForm:TForm):Boolean; var i:Integer; FormExist:Boolean; begin if(WForm=nil) then begin Result:=False; end; for i:=0 to Screen.FormCount do begin if(Screen.Forms[i].ClassType=WForm.ClassType) then Break; FormExist:=True; end; if(FormExist=False) then Result :=FormExist; if(WForm.WindowState=wsMinimized) then begin ShowWindow(WForm.Handle,SW_SHOWNORMAL); end else ShowWindow(WForm.Handle,SW_SHOWNA); if(not WForm.Visible) then WForm.Visible:=True; WForm.BringToFront ; WForm.SetFocus; Result := True; end; //这是函数调用 procedure TdmAnFina.DataModuleCreate(Sender: TObject); begin if(OpenForm(frLogin)=False)then frLogin := tfrLogin.Create(Application); frLogin.Show; end;
----------------------------------------------
Shift your life
procedure ShowChildForm; begin if not Assigned(ChildForm) then ChildForm := TChildForm.Create(MainForm); ChildForm.Show; end;
----------------------------------------------
if not Assigned(ChildForm) then ChildForm := TChildForm.Create(MainForm); ChildForm.Show; 好象不行,如果我關閉childform﹐再創建就不行了
----------------------------------------------
-
按sephil的方法可以解決 procedure TForm1.Button1Click(Sender: TObject); begin if not assigned(form2) then form2:=Tform2.Create(self); form2.Show; end; 在project->options->將form2移到右框 procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction); begin action:=cafree; end;
procedure TForm2.FormDestroy(Sender: TObject); begin form2:=nil; end;
----------------------------------------------
-
一个更简单的解决办法: if(frLogin=nil) then begin frLogin=TfrLogin.Create(self); end; frLogin.Show; 因为窗体本身就是一个指针;所以判断是不是nil就可以了,不过在子窗体关闭时要将窗体指针归零:frLogin:=nil;不然会出错的 procedure TvfrLogin.FormClose(Sender: TObject; var Action: TCloseAction); begin action:=cafree; frLogin:=nil; end;
----------------------------------------------
Shift your life