您的位置:首页 > 其它

BCB动态创建窗口的释放

2006-07-11 10:20 260 查看
1. ShowModal方法

TForm1 *Form=new TForm1(Application);
Form->ShowModal();
delete Form;
Form=NULL;

2. Show方法

TForm1 *Form=new TForm1(Application);
Form->Show();

在TForm1的OnClose事件中
设置Action=caFree;
就可以了,不需要delete的。

3.对于MDICHILD窗口不用显示删除,程序在结束时会自动删除。

VCL说明

以下信息来自pazee (耙子)

http://topic.csdn.net/t/20020809/06/929430.html

窗口用单纯的close是不能释放窗口的,
很多时候我们不写 delete xxx 来释放,而是在TForm::OnClose的事件里面写上 Action= caFree; 来实现在关闭窗口的同时自动释放。
这个用法一般都用在 非模态窗口,因为这个时候窗口的生命周期不好控制,比如我们用xxx->Show(); 出来的窗口(MDI尤其这样)
为了说明它的确可以释放,我们看看VCL源代码

TForm继承自TCustomForm,

procedure TCustomForm.CloseModal;
var
CloseAction: TCloseAction;
begin
try
CloseAction := caNone; // 初始化Action 为csNone;
if CloseQuery then // 执行CloseQuery 就是 Form的OnCloseQuery,从这里可以知道OnCloseQuery事件比OnClose先发生
begin
CloseAction := caHide;
if Assigned(FOnClose) then FOnClose(Self, CloseAction); // 判断我们是否在OnClose事件里面填写了代码,有,则执行。注意这个CloseAction,他是Var 声明的,其参数会带回来的
end;
case CloseAction of
caNone: ModalResult := 0;
caFree: Release; // 我们关心的,如果我们在OnClose里面写了Action= caFree 就会执行这句,
end;
except
ModalResult := 0;
Application.HandleException(Self);
end;
end;

TCustomForm.Release 的说明:
Destroys the form and frees its associated memory
他彻底的释放了内存,你就不用delete 他了。

我们一般用delete 都是对模态的窗口操作,因为他的窗口生命周期是可知的,比如楼上说的
xx->ShowModal();
delete xx;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: