【问题标题】:Focus on Modal Dialog (MFC)专注于模态对话框(MFC)
【发布时间】:2013-09-22 19:44:55
【问题描述】:

我像这样创建模态对话框:

CDialog dlg;
dlg.DoModal();

但是当窗口打开时,我可以访问程序的背景窗口(移动它们并关闭它们),但我只需要关注我当前的窗口。 (我认为模态对话框不应该是这样的)

我该怎么做?

编辑:

似乎我找到了这种行为的原因:在打开我的对话框之前,我在 CMyDlg::OnInitDialog() 函数中打开了另一个模态对话框,当我评论这个时,我的对话框再次变成模态。但是如何解决这个问题呢?

一些描述问题的代码:

void CMyView::OnSomeButtonPress() 
{
    CMyDlg dlg;
    dlg.DoModal();
}

BOOL CMyDlg::OnInitDialog() 
{
    CDialog::OnInitDialog();

    //some init here...


    //new modal dialog here (if comment this CMyDlg works as modal)
    CSettingsDlg dlg;
    dlg.DoModal();

    //...
 }

【问题讨论】:

  • 这听起来像是父窗口问题。可能由于某种原因,您的对话框的父窗口不正确。如果不查看更多代码,很难说出发生了什么。它是什么类型的 MFC 应用程序?基于对话框? MDI、SDI、...?
  • 是MDI,是什么“父窗口问题”?
  • 好的,现在很清楚了。如果您提出问题,请尽量提供最多的信息。否则你不会得到答案,你会得到错误的答案,或者人们会要求你提供更多信息(就像我一样)。

标签: visual-c++ user-interface mfc dialog modal-dialog


【解决方案1】:

你可以通过为对话框指定父窗口来解决你的问题,你可以通过在每个对话框类的构造函数中传递这个指针来解决你的问题,如代码所示。

void CMyView::OnSomeButtonPress()
{
    CMyDlg dlg(this);
    dlg.DoModal();
}

BOOL CMyDlg::OnInitDialog() 
{
     CDialog::OnInitDialog();

    //some init here...
    CSettingsDlg dlg(this);
    dlg.DoModal();

    //...
 }

【讨论】:

    【解决方案2】:

    您不能从 OnInitDialog 方法或从 OnInitDialog 方法调用的任何函数中使用对话框。您必须从其他地方使用 CSettingsDlg 的 DoModal()。

    类似这样的:

    void CMyView::OnSomeButtonPress() 
    {
        //new modal dialog here (if comment this CMyDlg works as modal)
        CSettingsDlg dlgSettings;
        dlgSettings.DoModal();
    
        ...
    
        CMyDlg dlg;
        dlg.DoModal();
    }
    
    BOOL CMyDlg::OnInitDialog() 
    {
        CDialog::OnInitDialog();
    
        //some init here...
    
        //...
     }
    

    【讨论】:

      猜你喜欢
      • 2013-08-26
      • 2010-09-21
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多