1,非模态对话框的创建
void CModelessDlg::OnButton1()
{
// TODO: Add your control notification handler code here
// 防止模态对话框重复创建
CWnd* pWnd = FindWindow(0, "非模态对话框的名称");
if(NULL == pWnd)
{
CDlgModeless* dlg = new CDlgModeless();
dlg->Create(IDD_MODELESS, this);
dlg->ShowWindow(SW_SHOW);
}
else
{
pWnd->BringWindowToTop();
}
}
2,非模态对话框的销毁,对非模态对话框类(CDlgModeless)的处理
a,重载Oncacle函数
// .h文件
virtual void OnCancel();
// .cpp文件
void CDlgModeless::OnCancel()
{
DestroyWindow();
// 注意:此处不能调用CDialog::OnCancel();
}
b,重载 PostNcDestroy函数
// .h文件
virtual void PostNcDestroy();
// .cpp文件
void CDlgModeless::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this; // 销毁对象
}