如何使用 do modal 关闭对话框
好的,基本上我有一个 MFC 应用程序,其中有很多需要循环的对话框。基本上,当您单击一个按钮转到另一个对话框时,我希望关闭前一个对话框。现在,对话框只是显示在彼此之上。新对话框打开后如何关闭对话框?这是一些示例代码:
void CMachine2Dlg::OnBnClickedNewmc()
{
NameDlg Dlg;
Dlg.DoModal()
}
Okay basically I have an MFC application with lots of dialogs that need to be cycled through. Basically when you click one button to go to another dialog, I want the previous dialog to close. Right now the dialogs are just displayed over the top of each other. How do I get the dialog to close once the new dialog has opened? Here is some example code:
void CMachine2Dlg::OnBnClickedNewmc()
{
NameDlg Dlg;
Dlg.DoModal()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以在对话框类中调用OnCancel()。例如:
this->OnCancel();
You could call OnCancel() inside your dialog class. Like:
this->OnCancel();
@tenfour提出了一个好的可能的解决方案
但如果这对你来说不可能
创建对话框
你应该从一个基本的窗口/Dlg等
......这样你就无法轻松控制正在发生的“什么”,并且你不必搞乱不同的窗口、消息循环。
@tenfour suggest a good possible solutions
But if its not possible for you
you should create the dialogs from one basic windows/Dlg
and so on....
This way you don't have easy controll of "what's" going on and you don't have to mess with different windows, messageloops.
您可以做的就是在
DoModal()
新对话框时隐藏父对话框,并在新对话框结束后销毁它。我还没有测试下面的ShowWindow()
但你明白了,如果它不隐藏对话框,请寻找另一个类似的函数。What you can do is hide the parent dialog when you
DoModal()
the new dialog and destroy it after the new dialog ends. I have not tested theShowWindow()
below but you get the idea, if it doesn't hide the dialog look for another similar function.它很难按照您提到的方式链接这些对话。 Do modal 通常意味着准确地实现您正在经历的事情。即:在上一个对话框上弹出对话框。
一种方法是在调用第一个对话框的类中创建模式对话框序列,并使用前一个对话框的返回值来确定是否需要显示第二个对话框,依此类推。
例如:
我相信你明白了......
在你的对话框中,你像这样返回(直接取自msf)
我会远离无模式对话框,你最终会遇到另一个问题,比如如何控制流程对话框并防止人们单击您应用程序后面的主窗口。
IT will be difficult to chain those dialog the way you mention. Do modal is usually meant to implement exactly what you are experiencing. Ie: dialog pops up over the previous one.
One way to do this is to create the modal dialogs sequence in the class that calls the first dialog and use the return value of the previous dialog to determine if you need to show the second one and so forth.
For ex:
I'm sure you get the idea...
in your dialog, you return like so (taken directly from msf)
I would stay clear of modeless dialog, you will end up with another problem like how to control the flow of dialog and prevent people to click the main window of your application behind.
你可以尝试打电话
You could try calling
OnOK()
、OnCancel()
或EndDialog(nResult)
将回答您的标题问题。但是,就像 @tenfour 建议的那样,您应该使用属性表/向导。它也可以是一个带有多个子属性页窗口的单个对话框窗口,您可以根据您想要看到的内容来显示或隐藏这些子属性页窗口。
为此,您将需要:
为对话框和每个属性页创建一个类,将每个属性页的成员变量添加到对话框中,创建属性页并使用框架作为放置它们的参考。单击按钮仅显示/隐藏必要的页面。
OnOK()
,OnCancel()
orEndDialog(nResult)
will answer your title question.However, like @tenfour suggested, you should be using a property sheet / wizard. It can also be a single dialog window with several child property page windows that you show or hide depending on what you want to be seen.
For this, you will need:
Create a class for the dialog and each property page, add a member variable of each property page to the dialog, create the property pages and use the frame as a reference to place them. On button click just show/hide the necessary pages.