1

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

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();

    //...
 }
4

2 回答 2

4

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

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

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

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

    //...
 }
于 2013-09-18T15:23:56.077 回答
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-09-18T13:21:51.403 回答