我编写了CDialog
基于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();
但它没有做任何事情。
然后我尝试调用::AllowSetForegroundWindow(ASFW_ANY);
从InitInstance
的应用程序,并没有做任何事情。
任何想法如何使应用程序启动时,第二个对话框前台?
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 }
我承认我没有测试过上面的代码,但是我看不出为什么你不能执行第一个对话,然后是第二个对话。