我试了下可以的
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);
procedure FormDestroy(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
T: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
T := TForm1.Create(nil);
T.SetBounds(0, 0, 100, 100); //区别主Form
T.ShowModal;
//T.Free; //因为是ShowModal,所以可以在这里Free
//T := nil;
{
T := TForm1.Create(nil);
try //最好用try..finally..end,可以保证T能正常释放
T.SetBounds(0, 0, 100, 100);
T.ShowModal;
finally
FreeAndNil(T);
end;
}
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Caption := BoolToStr(Assigned(T), True);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
T := nil;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
end.