非模态对话框的销毁

非模态对话框相对于模态对话框,他的创建和销毁过程和模态对话框有一定的区别

先看一下MSDN的原文:

When you implement a modeless dialog box, always override the OnCancel member function and call DestroyWindow from within it. Don’t call the base class CDialog::OnCancel, because it calls EndDialog, which will make the dialog box invisible but will not destroy it. You should also override PostNcDestroy for modeless dialog boxes in order to delete this, since modeless dialog boxes are usually allocated with new. Modal dialog boxes are usually constructed on the frame and do not need PostNcDestroy cleanup.

MS的指示:非模态对话框需要重载函数OnCanel,并且在这个函数中调用DestroyWindow。并且不能调用基类的OnCancel,因为基类的OnCancel调用了EndDialog这个函数,这个函数是针对模态对话框的。
还有一个必须重载的函数就是PostNcDestroy,这也是一个虚函数,通常的非模态对话框是用类的指针,通过new创建的,这就需要在PostNcDestroy函数中delete掉这个指针。

了解了理论过后,下面我们就可以用代码实现一下非模态对话框的创建和销毁过程:
//建立
//主框架中:
CTestDlg *pDlg=new CTestDlg;
pDlg->Create(IDD_TESTDLG,this);
pDlg->ShowWindow(SW_SHOW);

//对话框中:
void CTestDlg::OnCancel()
{
DestroyWindow();
}

void CTestDlg::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this;
}

如果要在点击按钮的情况下,销毁非模态对话框,只需要把按钮的事件映射到OnCancel函数即可。

 

其实上面没说明原因,原因是对于非模态对话框,其对象是创建在堆中的

关于各种窗口销毁过程参见

http://blog.youkuaiyun.com/geeeeeeee/archive/2009/01/02/3686093.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值