1
votes

I create modal dialog like this :

CDialog dlg;
dlg.DoModal();

but when window opens I can access background windows of my program (move them and close them) but I need focus only on my curent window. (I think modal dialog shouldn't behave like this)

How can I do this?

EDIT:

It seems I found the reason of this behavior: before open my dialog,I open another modal dialog in CMyDlg::OnInitDialog() function, when I comment this,my dialog again became modal. But how to solve this problem?

Some code describing problem:

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

    //...
 }
2
This sounds like a parent window problem. Probably for some reason the parent window of your dialog is not correct. It's hard to tell what happend without seeing more code. What kind of MFC appllication is it ? Dialog based ? MDI, SDI,... ? - Jabberwocky
It's MDI, what is it "parent window problem"? - mrgloom
OK, now it is clear. If you ask questions try to provide a maximum of information. Otherwise you won't get an answer, you will get wrong answers or people will ask you to provide more information (just as I did). - Jabberwocky

2 Answers

4
votes

you can solve your problem by specifying parent window for the dialog, you can do by passing this pointer in constructor of each dialog class as shown in code .

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

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

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

    //...
 }
2
votes

You cannot use a dialog from within the OnInitDialog method or from any function called from the OnInitDialog method. You have to use the DoModal() of CSettingsDlg from else where.

Something like this:

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

    //...
 }