专注于模态对话框(MFC)(Focus on Modal Dialog (MFC))

2019-09-29 05:55发布

我创建模态对话框是这样的:

CDialog dlg;
dlg.DoModal();

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

我怎样才能做到这一点?

编辑:

看来我发现这种现象的原因:前打开我的对话,我打开的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();

    //...
 }

Answer 1:

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

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

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

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

    //...
 }


Answer 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...

    //...
 }


文章来源: Focus on Modal Dialog (MFC)