function Findform(formname: string): boolean; //查找名为formname的窗体是否存在,存在返回true var fi: integer; begin result := false; for Fi := 0 to screen.Formcount - 1 do if uppercase(screen.forms[Fi].Name) = uppercase(formname) then begin screen.forms[Fi].BringToFront; result := true; break; end else result := false; end;
if not XXXX.findform('Fm_XXX') then Fm_XXX := TFm_XXX.create(application);
procedure TMainForm.N10Click(Sender: TObject); var FrmFindGroup :TFrmFindGroup; begin FrmFindGroup :=TFrmFindGroup(Application.FindComponent('FrmFindGroup')); if not(Assigned(FrmFindGroup)) then begin FrmFindGroup :=TFrmFindGroup.create(Application); end; FrmFindGroup.show;end;
看看我的 function FindChildForm(aFormClass: TFormClass): TForm; var i: integer; begin Result := nil; for i := 0 to Application.MainForm.MDIChildCount - 1 do begin if Application.MainForm.MDIChildren[i].ClassName = aFormClass.ClassName then begin Result := Application.MainForm.MDIChildren[i]; Break; end; // end if end; // end for end;
procedure TForm1.SpeedButton2Click(Sender: TObject); begin if not Assigned(form3) then Form3:=TForm3.Create(nil); Form3.Show; end; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction); begin action:=caFree; end;procedure TForm3.FormDestroy(Sender: TObject); begin form3:=nil; end;
呵~~,不用这么麻烦吧:) 1、创建窗体时: if frmchild1=nil then application.createform(tfrmchild1,frmchild1) else begin frmchild1.show; frmchild1.bringfront;//---方法名可能不对,:) end; 2、关闭子窗体时: ONCLOSE() BEGIN action:=cafree; frmchild1:=nil;//---将窗体指针清0 END;
function IsExistsFormName(fmName:string): boolean; Var i:integer; begin result:=false; with application.MainForm do begin for I := 0 to MDIChildCount - 1 do begin if uppercase(Trim(MDIChildren[I].Name))=uppercase(Trim(fmName)) then begin result:=true; MDIChildren[I].WindowState:=wsnormal; MDIChildren[I].BringToFront; exit; end; end; end; end;
MDI子窗体创建会发送一个WM_MDICREATE消息,我们可以通过这个消息的返回值Result得到新创建窗体的Handle,然后通过一个List来维护这个Handle列表,就可以判断了。例如: WM_MDICREATE: begin Default; FMDIHandle:= Message.Result; FHandle.Add(IntToStr(FMDIHandle)); end; ...
var
fi: integer;
begin
result := false;
for Fi := 0 to screen.Formcount - 1 do
if uppercase(screen.forms[Fi].Name) = uppercase(formname) then
begin
screen.forms[Fi].BringToFront;
result := true;
break;
end
else
result := false;
end;
Fm_XXX := TFm_XXX.create(application);
var
FrmFindGroup :TFrmFindGroup;
begin
FrmFindGroup :=TFrmFindGroup(Application.FindComponent('FrmFindGroup'));
if not(Assigned(FrmFindGroup)) then
begin
FrmFindGroup :=TFrmFindGroup.create(Application);
end;
FrmFindGroup.show;end;
function FindChildForm(aFormClass: TFormClass): TForm;
var
i: integer;
begin
Result := nil;
for i := 0 to Application.MainForm.MDIChildCount - 1 do
begin
if Application.MainForm.MDIChildren[i].ClassName
= aFormClass.ClassName then
begin
Result := Application.MainForm.MDIChildren[i];
Break;
end; // end if
end; // end for
end;
procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
if not Assigned(form3) then Form3:=TForm3.Create(nil);
Form3.Show;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
action:=caFree;
end;procedure TForm3.FormDestroy(Sender: TObject);
begin
form3:=nil;
end;
1、创建窗体时:
if frmchild1=nil then
application.createform(tfrmchild1,frmchild1)
else
begin
frmchild1.show;
frmchild1.bringfront;//---方法名可能不对,:)
end;
2、关闭子窗体时:
ONCLOSE()
BEGIN
action:=cafree;
frmchild1:=nil;//---将窗体指针清0
END;
Var i:integer;
begin
result:=false;
with application.MainForm do
begin
for I := 0 to MDIChildCount - 1 do
begin
if uppercase(Trim(MDIChildren[I].Name))=uppercase(Trim(fmName)) then
begin
result:=true;
MDIChildren[I].WindowState:=wsnormal;
MDIChildren[I].BringToFront;
exit;
end;
end;
end;
end;
WM_MDICREATE:
begin
Default;
FMDIHandle:= Message.Result;
FHandle.Add(IntToStr(FMDIHandle));
end;
...