非模态对话框的销毁
2008-12-31 00:36
190 查看
非模态对话框相对于模态对话框,他的创建和销毁过程和模态对话框有一定的区别
先看一下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.csdn.net/geeeeeeee/archive/2009/01/02/3686093.aspx
先看一下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.csdn.net/geeeeeeee/archive/2009/01/02/3686093.aspx
相关文章推荐
- 再谈非模态对话框的销毁 / 对话框建立过程/ 对话框销毁过程/ 消息传递过程
- MFC之模态与非模态对话框的创建与销毁
- C++非模态对话框自动销毁的好方法
- VC非模态对话框创建和销毁
- 非模态对话框销毁的问题,SldWorks二次开发的朋友可以看看
- 模态对话框与非模态对话框的创建及的几种销毁方法与区别
- MFC非模态对话框的销毁(转)
- VC中非模态对话框相对于模态对话框的创建和销毁过程
- VC非模态对话框创建和销毁
- VC非模态对话框创建和销毁
- VC非模态对话框创建和销毁
- 非模态对话框 建立 销毁
- MFC非模态对话框销毁
- 非模态对话框 模态对话框 --创建和销毁过程
- 谈非模态对话框的销毁 作者:cooldog 日期:2006-02-23
- MFC非模态对话框的销毁(转)
- 非模态对话框失去焦点后销毁对话框
- C++非模态对话框自动销毁的好方法
- 模态和非模态对话框的创建于销毁过程
- VC非模态对话框创建和销毁