VC中关于模态和非模态对话框的疑问,该怎么解决
VC中关于模态和非模态对话框的疑问
首先创建一个基于对话框的MFC应用程序,主对话框上有两个按钮,一个按钮用于打开一个模态对话框,另一个用于打开一个非模态对话框。
然后按Button1打开一个非模态对话框,
然后按Button3打开一个模态对话框,
这时的模态对话框只对主窗口起作用,而对之前打开的非模态对话框则不显现模态特性,可关闭非模态对话框,
这时的模态对话框对主窗口也不起作用了,此时则可以将主对话框关闭,
此时的模态对话框并未因为主窗口的关闭而消失,似乎已经脱离了主程序的束缚了!!!
实在是不明白是什么原因!!!敬请各位指点!!!
------解决方案--------------------
你的非模态对话框并没有关闭,只是隐藏了。非模态对话框隐藏的时候,可能EnableWindow了父窗口。你可以试试真正的关闭非模态对话框,而不仅仅是隐藏它。
关闭非模态对话框参考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.
------解决方案--------------------
非模态对话框的消息需要在PostNcDestroy 中delete this
------解决方案--------------------
首先创建一个基于对话框的MFC应用程序,主对话框上有两个按钮,一个按钮用于打开一个模态对话框,另一个用于打开一个非模态对话框。
然后按Button1打开一个非模态对话框,
然后按Button3打开一个模态对话框,
这时的模态对话框只对主窗口起作用,而对之前打开的非模态对话框则不显现模态特性,可关闭非模态对话框,
这时的模态对话框对主窗口也不起作用了,此时则可以将主对话框关闭,
此时的模态对话框并未因为主窗口的关闭而消失,似乎已经脱离了主程序的束缚了!!!
实在是不明白是什么原因!!!敬请各位指点!!!
------解决方案--------------------
你的非模态对话框并没有关闭,只是隐藏了。非模态对话框隐藏的时候,可能EnableWindow了父窗口。你可以试试真正的关闭非模态对话框,而不仅仅是隐藏它。
关闭非模态对话框参考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.
------解决方案--------------------
非模态对话框的消息需要在PostNcDestroy 中delete this
------解决方案--------------------