定义一个窗口实例公用变量
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
fm2: TForm2;
//在这里定义implementation{$R *.dfm}end.
然后让其它的窗口不自动创建。
这时在菜单里这样写
if fm2<>nil then
fm2:=Tform2.create(self);
fm2.show;
就可以了
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
fm2: TForm2;
//在这里定义implementation{$R *.dfm}end.
然后让其它的窗口不自动创建。
这时在菜单里这样写
if fm2<>nil then
fm2:=Tform2.create(self);
fm2.show;
就可以了
让其它的mdi子窗口不自动创建
注意Tform2是一个mdi子窗口。
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 (quan=1) then
begin
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;
end;
Child:=TForm(FormClass.NewInstance);
TForm(fm):=Child;
Child.Create(AOwner);
end;
if Assigned(form1) then //判断form1是否为空,是空就创建。
begin
form1:=TForm1.Create(self);
form1.Show;
end
else
form1.show;
在关闭窗体时要释放
action:=cafree;
form1:=nil;
TForm1=class(TForm)
...
public
FChild:TChildForm;
...
end;创建子窗体时:
if FChild <> nil then
FChild := TChildForm.Create(self);
...在ChildForm的OnClose事件中:
implementation
uses unit;//主窗体的单元.
...
TChildForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
MainForm.FChild := nil;
end;