BCB动态创建窗口的释放

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值