模式对话框创建后,程序的其他窗口便不能进行操作,必须将该窗口关闭后,其他窗口才能进行操作。而非模式对话框则无需这样,它不强制要求用户立即反应,而是与其他窗口同时接受用户操作。
模态对话框创建和销毁
模式对话框一般是使用DoModal创建的,响应IDOK和IDCANCEL进行销毁,如:
<span style="background-color: rgb(0, 0, 0);"> </span><span style="font-family:SimSun;"></span><span style="font-family:SimSun;">CTestDlgdlg;</span><p style="margin-bottom: 0cm;" align="LEFT"><span style="font-family:SimSun;">inti;</span></p><p style="margin-bottom: 0cm;" align="LEFT"><span style="font-family:SimSun;">i= dlg.DoModel();</span></p><p style="margin-bottom: 0cm;" align="LEFT"><span style="font-family:SimSun;">if(i == IDOK)</span></p><p style="margin-bottom: 0cm;" align="LEFT"><span style="font-family:SimSun;">{</span></p><p style="margin-bottom: 0cm;" align="LEFT"><span style="font-family:SimSun;"> //do something</span></p><p style="margin-bottom: 0cm;" align="LEFT"><span style="font-family:SimSun;">}</span></p><p style="margin-bottom: 0cm;" align="LEFT"><span style="font-family:SimSun;">elseif(i == IDCANCEL)</span></p><p style="margin-bottom: 0cm;" align="LEFT"><span style="font-family:SimSun;">{</span></p><p style="margin-bottom: 0cm;" align="LEFT"><span style="font-family:SimSun;"> //do other something</span></p><p style="margin-bottom: 0cm;" align="LEFT"><span style="font-family:SimSun;">}</span> </p>
非模态对话框创建和销毁
非模式对话框是使用Create创建的,非模态对话框需要重载函数OnCanel,并且在这个函数中调用DestroyWindow;并且不能调用基类的OnCancel,因为基类的OnCancel调用了EndDialog这个函数,这个函数是针对模态对话框的。
还有一个必须重载的函数就是PostNcDestroy,这也是一个虚函数,通常的非模态对话框是用类的指针,通过new创建的,这就需要在PostNcDestroy函数中delete掉这个指针。如果要在点击按钮的情况下,销毁非模态对话框,只需要把按钮的事件映射到OnCancel函数即可。
例子:
//建立 //主框架中: CTestDlg*pDlg=newCTestDlg(); pDlg->Create(IDD_TESTDLG,this); pDlg->ShowWindow(SW_SHOW); //销毁 //对话框中: voidCTestDlg::OnCancel() { DestroyWindow(); } voidCTestDlg::PostNcDestroy() { CDialog::PostNcDestroy(); delete this; }
模态和非模态对话框
最新推荐文章于 2023-10-26 20:18:37 发布