非模态对话框相对模态对话框的创建和释放都相对繁琐点。研究一下非模态对话框的释放问
题:
Modal dialog boxes are normally created on the stack frame and destroyed when the function that
created them ends. The dialog object’s destructor is called when the object goes out of scope.
Modeless dialog boxes are normally created and owned by a parent view or frame window — the
application’s main frame window or a document frame window. The default OnClose handler
calls DestroyWindow, which destroys the dialog-box window. If the dialog box stands alone,
with no pointers to it or other special ownership semantics, you should override PostNcDestroy to
destroy the C++ dialog object. You should also override OnCancel and call DestroyWindow from
within it. If not, 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 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.
1、非模态对话框是在堆中产生,必须要释放;模态对话框在栈中,过了作用域会自动释放
2 、 此 时 用 IDOK,IDCANCEL 关 闭 窗 体 时 , 应 对 OnOK,OnCancel 重 写 ,
CDialog::OnOK();CDialog::OnCancel();应被替换为 CDialog::DestroyWindow()。因为前者是会
条用 CDialog::EndDialog, 其是为模态对话框而设计,非模态调用它只能隐藏窗体而不会释
放。
/ / TODO: Add extra validation here
CDialog::DestroyWindow();
3、如果对话框为游离状态(指父窗体或其他窗体没有掌控指向该对话框的指针),就需要重
载 PostNcDestroy
void CTestDlg::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this;
//据说此函数在基类什么都没做
//在此释放窗体内存块