D7 关于窗体调用的问题(100分)

  • 主题发起人 主题发起人 pengcq2008
  • 开始时间 开始时间
P

pengcq2008

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个主窗体form1 有两个被调用的窗体form2,form3 , form1可以调用form2,也可以调用form3,form2与form3可以相互多次调用,但是最后一个被调用出来的窗体退出后要直接回到主窗体form1,比如Form2最后一次调用了Form3,则Form3退出后直接回到主窗体Form1, 我做了一个简单事例程序,这个程序不会报错,但是Form2,Form3被调用一次,就创建一次,调用多个次就会出多个窗口,很占用资源,这样要关闭多次才能关闭完Form2,Form3.(有个方法就是要判断被调用的窗体是否存在,如果不存在则创建该窗体,否则直接显示出来.这个方法可以考虑),下面是例程的代码: 谢谢大家帮忙!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
uses unit2,unit3 ;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if not Assigned(Form2) then
begin
Form2:=TForm2.Create(nil);
try
Form2.ShowModal;
finally
FreeAndNil(Form2);
end;
end
else
Form2.ShowModal;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if not Assigned(Form3) then
begin
Form3:=TForm3.Create(nil);
try
Form3.ShowModal;
finally
FreeAndNil(Form3);
end;
end
else
Form3.ShowModal;
end;

end.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;
implementation
uses unit3;
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
// if Assigned(Form3) then
// FreeAndNil(Form3);
// if not Assigned(Form3) then
begin
Form3:=TForm3.Create(nil);
try
Form3.ShowModal;
finally
FreeAndNil(Form3);
end;
end ;
{ else
begin
FreeAndNil(Form3);
Form3.ShowModal;
end;
}
end;

procedure TForm2.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action:=caFree;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
Form3:=nil;
end;

end.
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form3: TForm3;
implementation
uses unit2;
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
// if Assigned(Form2) then
// FreeAndNil(Form2);
// if not Assigned(Form2) then
begin
Form2:=TForm2.Create(nil);
try
Form2.ShowModal;
finally
FreeAndNil(Form2);
end;
// end
{ else
begin
FreeAndNil(Form2);
Form2.ShowModal;
}
end;

end;

procedure TForm3.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action:=caFree;
end;

procedure TForm3.FormDestroy(Sender: TObject);
begin
Form3:=nil;
end;

end.
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2},
Unit3 in 'Unit3.pas' {Form3};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
 
楼主试下这样:
先定义一个过程
procedure OpenForm(FormClass: TFormClass;
var fm;
AOwner: TComponent);
var
i: integer;
Child: TForm;
begin
for i := 0 to Screen.FormCount - 1do
if Screen.Forms.ClassType = FormClass then
begin
Child := Screen.Forms;
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;
Child := TForm(FormClass.NewInstance);
TForm(fm) := Child;
Child.Create(AOwner);
end;

//调用这个过程来打开窗体
procedure TMainForm.N4Click(Sender: TObject);
var
Child: TfmForm1;
begin
OpenForm(TfmForm1, Child, Self);
end;
 
form2、form3不要close,用hide,form1的OnClose事件中用Halt
 
lngdtommy 这个只是打开窗体之前,判断窗体是否存在,但是被调用的窗体关闭时,还是要写Action:=caFree;
??
 
多人接受答案了。
 

Similar threads

后退
顶部