标签:
A modal dialog box closes automatically when the user presses the OK or Cancel buttons or when your code calls theEndDialog member function.
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.
所以我们也按照他们的建议进行操作,
void CMyDlg::OnOK() { // TODO: 在此添加专用代码和/或调用基类 DestroyWindow(); return; //CDialogEx::OnOK(); } void CMyDlg::OnCancel() { // TODO: 在此添加专用代码和/或调用基类 DestroyWindow(); return; //CDialogEx::OnCancel(); } void CMyDlg::PostNcDestroy() { // TODO: 在此添加专用代码和/或调用基类 CDialogEx::PostNcDestroy(); delete this; }
注:如果你不是像以上那样临时new一个的话,比如你可能会让它成为一个类成员对象,然后在初始化的时间创建它,在类销毁时自动销毁此模态对话框的话,那么就不应该在PostNcDestroy处delete它,也不应当DestroyWindow()它,会引发内存操作错误。因为此时它并不是new出来的。比如:在类A中有一个成员变量为CMyDlg
m_mMyDlg; 然后在类构造函数中调用m_mMyDlg.Create( IDC_MY_DLG);
作者:山丘儿
转载请标明出处,谢谢。原文地址:http://blog.csdn.net/s634772208/article/details/46404761
标签:
原文地址:http://blog.csdn.net/s634772208/article/details/46404761