如何使用 do modal 关闭对话框

发布于 2024-12-11 18:44:06 字数 230 浏览 0 评论 0原文

好的,基本上我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

叹沉浮 2024-12-18 18:44:07

您可以在对话框类中调用OnCancel()。例如:this->OnCancel();

You could call OnCancel() inside your dialog class. Like: this->OnCancel();

指尖上的星空 2024-12-18 18:44:07

@tenfour提出了一个好的可能的解决方案
但如果这对你来说不可能
创建对话框

Mydialog  dlg1
if(dlg1.DoModal() )
{
   //do something
}
else
   // do something else

Mydialog  dlg2
if(dlg2.DoModal() )
{
   //do something
}
else
   // do something else

你应该从一个基本的窗口/Dlg等

......这样你就无法轻松控制正在发生的“什么”,并且你不必搞乱不同的窗口、消息循环。

@tenfour suggest a good possible solutions
But if its not possible for you
you should create the dialogs from one basic windows/Dlg

Mydialog  dlg1
if(dlg1.DoModal() )
{
   //do something
}
else
   // do something else

Mydialog  dlg2
if(dlg2.DoModal() )
{
   //do something
}
else
   // do something else

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.

任谁 2024-12-18 18:44:06

您可以做的就是在 DoModal() 新对话框时隐藏父对话框,并在新对话框结束后销毁它。我还没有测试下面的 ShowWindow() 但你明白了,如果它不隐藏对话框,请寻找另一个类似的函数。

void CMachine2Dlg::OnBnClickedNewmc()
{
   ShowWindow( SW_HIDE);

   NameDlg Dlg;
   Dlg.DoModal();

   EndDialog( 0 );

}

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 the ShowWindow() below but you get the idea, if it doesn't hide the dialog look for another similar function.

void CMachine2Dlg::OnBnClickedNewmc()
{
   ShowWindow( SW_HIDE);

   NameDlg Dlg;
   Dlg.DoModal();

   EndDialog( 0 );

}
挽清梦 2024-12-18 18:44:06

它很难按照您提到的方式链接这些对话。 Do modal 通常意味着准确地实现您正在经历的事情。即:在上一个对话框上弹出对话框。

一种方法是在调用第一个对话框的类中创建模式对话框序列,并使用前一个对话框的返回值来确定是否需要显示第二个对话框,依此类推。

例如:

// define a bunch of constants, any number would do,
// I would avoid 0 and 1 as they usually mean success/error
// This code can be returned in the EndDialog call in a method of your choice (say button click handler).

const int c_needNextDialog = 101; 


dialog1 dlg1;

if( dlg1.DoModal() == c_needNextDialog )
{
   dialog2 dlg2;

   if( dlg2.DoModal() == c_needNextDialog )
   {
      ... and so forth
   }
}

我相信你明白了......

在你的对话框中,你像这样返回(直接取自msf)

void dialog1::OnSomeAction()
{
   // Do something
   EndDialog(c_needNextDialog); // This value is returned by DoModal!

   // Do something

   return; // Dialog closed and DoModal returns only here!
}

我会远离无模式对话框,你最终会遇到另一个问题,比如如何控制流程对话框并防止人们单击您应用程序后面的主窗口。

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:

// define a bunch of constants, any number would do,
// I would avoid 0 and 1 as they usually mean success/error
// This code can be returned in the EndDialog call in a method of your choice (say button click handler).

const int c_needNextDialog = 101; 


dialog1 dlg1;

if( dlg1.DoModal() == c_needNextDialog )
{
   dialog2 dlg2;

   if( dlg2.DoModal() == c_needNextDialog )
   {
      ... and so forth
   }
}

I'm sure you get the idea...

in your dialog, you return like so (taken directly from msf)

void dialog1::OnSomeAction()
{
   // Do something
   EndDialog(c_needNextDialog); // This value is returned by DoModal!

   // Do something

   return; // Dialog closed and DoModal returns only here!
}

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.

青衫负雪 2024-12-18 18:44:06

你可以尝试打电话

EndDialog(nResult);

You could try calling

EndDialog(nResult);
终陌 2024-12-18 18:44:06

OnOK()OnCancel()EndDialog(nResult) 将回答您的标题问题。

但是,就像 @tenfour 建议的那样,您应该使用属性表/向导。它也可以是一个带有多个子属性页窗口的单个对话框窗口,您可以根据您想要看到的内容来显示或隐藏这些子属性页窗口。

为此,您将需要:

  • 1 个对话框窗口,可能带有“上一个”/“下一个”按钮
  • 1 个图片框,框架样式,不可见,位于您希望子窗口出现的对话框内
  • n 属性页,子样式,无边框,您可以在其中显示子窗口把所有的控制。

为对话框和每个属性页创建一个类,将每个属性页的成员变量添加到对话框中,创建属性页并使用框架作为放置它们的参考。单击按钮仅显示/隐藏必要的页面。

OnOK(), OnCancel() or EndDialog(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:

  • 1 dialog window, maybe with Prev/Next buttons
  • 1 picture box, frame style, not visible, inside the dialog where you want the child windows to appear
  • n property pages, child style, no border, where you put all the controls.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文