c++ - MFC C++ 中的对话框

标签 c++ mfc

我在 1 个项目中有 4 个对话框。让我们称呼他们

IDD_DIALOG1
IDD_DIALOG2
IDD_DIALOG3
IDD_DIALOG4

当我编译我的程序时,我能看到的第一个窗口/对话框是 IDD_DIALOG1,但我想先有 IDD_DIALOG2。 我的项目没有类似 WinMain 的东西。这是明确的 MFC 应用程序。

最佳答案

当您创建项目时,您也会有一个application 类。在这个类中已经有一个默认值 InitInstance方法。例如:

BOOL CMFCApplication1App::InitInstance()
{
    // InitCommonControlsEx() is required on Windows XP if an application
    // manifest specifies use of ComCtl32.dll version 6 or later to enable
    // visual styles.  Otherwise, any window creation will fail.
    INITCOMMONCONTROLSEX InitCtrls;
    InitCtrls.dwSize = sizeof(InitCtrls);
    // Set this to include all the common control classes you want to use
    // in your application.
    InitCtrls.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&InitCtrls);

    CWinApp::InitInstance();


    AfxEnableControlContainer();

    // Create the shell manager, in case the dialog contains
    // any shell tree view or shell list view controls.
    CShellManager *pShellManager = new CShellManager;

    // Activate "Windows Native" visual manager for enabling themes in MFC controls
    CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

    // 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
    // Change the registry key under which our settings are stored
    // TODO: You should modify this string to be something appropriate
    // such as the name of your company or organization
    SetRegistryKey(_T("Local AppWizard-Generated Applications"));

    CMFCApplication1Dlg 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 if (nResponse == -1)
    {
        TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n");
        TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
    }

    // Delete the shell manager created above.
    if (pShellManager != NULL)
    {
        delete pShellManager;
    }

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

该方法的一部分是这段代码:

CMFCApplication1Dlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();

这是您感兴趣的主要代码。因此,如果您想从不同的对话开始,请#include 正确的标题并将代码更改为不同的对话类。

因此在上面的示例中,CMFCApplication1Dlg 将更改为其他内容,例如:CMyDialog2(我不知道您的对话类的名称是什么)。

关于c++ - MFC C++ 中的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37416154/

相关文章:

c++ - 数组元素的地址c++

visual-studio-2012 - 过程入口点 `GetTickCount64` 无法在动态链接库 KERNEL32.dll 中定位

mfc - CDocument::SetPathName(..) 在 VS2015 中崩溃

c++ - 在主对话框中嵌入对话框并在 MFC 中单击按钮切换它们

c++ - 为什么我的 CTreeCtrl 复选框没有选中?

c++ - 无法创建主窗口?

c++ - 在 Visual Studio 中观察 C++ 数组

c++ - 为什么这些模板化函数不能不带参数?

c++ - CentOS 上的 Autoconf AC_CHECK_SIZEOF 始终为 0

python - SWIG:如何使用 %apply 返回结构? 'No typemaps defined' 警告