if not Assigned(form1) then form1:=TForm1.Create(Self); form1.show;在form1 窗口关闭时 OnClose加上以下代码: if Assigned(form1) then begin form1.free; form1:=nil; end;
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TmyForms = class of TForm; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } procedure ShowForm(TForms: TmyForms;var Form: TForm); public { Public declarations } end;var Form1: TForm1;implementation {$R *.dfm} uses Unit2,Unit3,Unit4;//Form2,Form3,Form4所在单元 var Form2,Form3,Form4:TForm;procedure TForm1.ShowForm(TForms: TmyForms;var Form: TForm); begin if not Assigned(Form) then //未建立 Form:=TForms.Create(self)//建立 else ShowWindow(Form.Handle,SW_SHOWNORMAL);//恢复原来尺寸 Form.Show;//显示出来 end;procedure TForm1.Button1Click(Sender: TObject); begin ShowForm(TForm2,Form2); end;procedure TForm1.Button2Click(Sender: TObject); begin ShowForm(TForm3,Form3); end;procedure TForm1.Button3Click(Sender: TObject); begin ShowForm(TForm4,Form4); end;end.
这样你就可以同时开任意数目的窗口了
form := TForm1.Create(Self);
form.Show;
如有个form1 form2 form3创建一个窗口按钮中加入如下代码。
if not Assigned(form1) then
form1:=TForm1.Create(Self);
form1.show;在form1 窗口关闭时 OnClose加上以下代码:
if Assigned(form1) then begin
form1.free;
form1:=nil;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TmyForms = class of TForm;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
procedure ShowForm(TForms: TmyForms;var Form: TForm);
public
{ Public declarations }
end;var
Form1: TForm1;implementation
{$R *.dfm}
uses Unit2,Unit3,Unit4;//Form2,Form3,Form4所在单元
var Form2,Form3,Form4:TForm;procedure TForm1.ShowForm(TForms: TmyForms;var Form: TForm);
begin
if not Assigned(Form) then //未建立
Form:=TForms.Create(self)//建立
else
ShowWindow(Form.Handle,SW_SHOWNORMAL);//恢复原来尺寸
Form.Show;//显示出来
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ShowForm(TForm2,Form2);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
ShowForm(TForm3,Form3);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
ShowForm(TForm4,Form4);
end;end.
就是这样的效果了