当基于 CDialog 的应用程序启动时,如何将我的辅助对话窗口置于顶部? [英] How to bring my secondary dialog window to the top when the CDialog-based app starts?

查看:52
本文介绍了当基于 CDialog 的应用程序启动时,如何将我的辅助对话窗口置于顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个基于 MFC CDialog 的应用程序.在正常情况下,它通过显示来自 InitInstance 处理程序的 CDialog 窗口来启动:

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

但是第一次运行这个应用程序时,我需要在主对话框出现在屏幕上之前从 CMyDialog::OnInitDialog 中显示另一个对话框.所以我做了类似的事情:

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

但是这种方法的问题是我的第二个 CIntroDialog 没有显示在前台.所以我试图通过从 CIntroDialog::OnInitDialog 中调用以下内容来解决这个问题:

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

但它没有做任何事情.

然后我尝试从应用程序的 InitInstance 调用 ::AllowSetForegroundWindow(ASFW_ANY);,但也没有做任何事情.

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?

附注.由于此应用程序的结构,我需要从 CMyDialog::OnInitDialog 中调用 CIntroDialog::DoModal 以防止大量重写.

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

推荐答案

您是否考虑在应用程序类中使用 InitInstance 来解决这个问题?

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.

这篇关于当基于 CDialog 的应用程序启动时,如何将我的辅助对话窗口置于顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆