How to bring my secondary dialog window to the top

2019-09-16 07:47发布

I've coded an MFC CDialog based application. In normal circumstances it starts up by displaying a CDialog window from the InitInstance handler as such:

CMyDialog dlg;
INT_PTR nResponse = dlg.DoModal();

But for the first time this app runs I need to display another dialog from within CMyDialog::OnInitDialog before the main dialog is on the screen. So I do a similar thing:

CIntroDialog idlg(this);
idlg.DoModal();

But the issue with this approach is that my second CIntroDialog is not displayed in the foreground. So I attempted to fix this by calling the following from within CIntroDialog::OnInitDialog:

    this->SetForegroundWindow();
    this->BringWindowToTop();

but it didn't do anything.

I then tried calling ::AllowSetForegroundWindow(ASFW_ANY); from InitInstance for the app, and that didn't do anything either.

Any idea how to bring that second dialog to the foreground when the app starts?

PS. Due to the structure of this app, I need to call CIntroDialog::DoModal from within CMyDialog::OnInitDialog to prevent an extensive rewrite.

1条回答
何必那么认真
2楼-- · 2019-09-16 08:16

Have you consider making use of InitInstance for this in the app class?

BOOL CMyApp::InitInstance()
{
    AfxEnableControlContainer();

    // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.

    CMyDlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with Cancel
    }

    // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
}

I have cut some of the default implementation out, but you see this bit:

CMyDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
}
else if (nResponse == IDCANCEL)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
}

There is nothing stopping you doing something like:

CMyDlg2 dlg2;

if(dlg2.DoModal() == IDOK)
{
    CMyDlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with Cancel
    }
}
else
{
    // Handle IDCANCEL
}

I admit I have not tested the above code, but I can't see why you can't execute the first dialogue and then the second dialogue.

查看更多
登录 后发表回答