基于CDialog的应用程序启动时,如何将我的辅助对话框窗口带到顶部

How to bring my secondary dialog window to the top when the CDialog-based app starts?

本文关键字:我的 对话框 窗口 顶部 应用程序 CDialog 启动 基于      更新时间:2023-10-16

我已经编码了基于MFC CDialog的应用程序。在正常情况下,它是通过从InitInstance处理程序显示CDialog窗口开始的:

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

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

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

但是,这种方法的问题是我的第二个CIntroDialog未显示在前景中。因此,我试图通过从CIntroDialog::OnInitDialog中调用以下内容来解决此问题:

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

,但没有做任何事情。

然后,我尝试从InitInstance调用::AllowSetForegroundWindow(ASFW_ANY);进行应用,但这也没有做任何事情。

当应用程序启动时如何将第二个对话框带到前景?

ps。由于该应用程序的结构,我需要从CMyDialog::OnInitDialog内调用CIntroDialog::DoModal,以防止大量重写。

您是否考虑在应用程序类中使用InitInstance

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;
}

我已经删除了一些默认实现,但是您会看到这一点:

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
}

没有什么可以阻止您做这样的事情:

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
}

我承认我尚未测试上述代码,但是我看不出为什么您不能执行第一次对话,然后是第二个对话。