c++ - 在 MFC 中显示模态对话框

标签 c++ mfc

我找不到任何关于 MFC 的好教程,我正在尝试显示一个对话框窗口。

我创建了一个新资源,向其中添加了我的控件,它继承自 CDialogEx,但我不知道应该将代码放在哪里来创建和显示对话窗口,我希望它在应用程序时加载开始,你能给我提示吗?

最佳答案

代码应该像这样在您的应用程序 InitInstance() 中:

BOOL MyApp::InitInstance()
{
    INITCOMMONCONTROLSEX InitCtrls;
    InitCtrls.dwSize = sizeof(InitCtrls);

    InitCtrls.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&InitCtrls);

    CWinApp::InitInstance();

    ExampleDlg dlg; // instance of dialog
    m_pMainWnd = &dlg; // make dialog main window
    INT_PTR nResponse = dlg.DoModal(); // get the response from your modal dialog 
    // this case, OK button, Cancel button or error in dialog
    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");
    }

这将在您启动应用程序并将其设为主窗口时为您提供一个对话框。这可以通过使用 MFC 应用程序向导并选择对话框库轻松完成。它会自动为您提供应用程序的布局。

如果你不想让它成为你的主窗口,只需使用:

ExampleDlg dlg;
dlg.DoModal();

然后让您的对话代码完成工作。

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

相关文章:

c++ - 控件不在非模式对话框 MFC 中呈现

c++ - 从 eeprom 接收数据时程序崩溃

c++ - 在 C++ 中静态地将用户输入获取到类中

c++ - 循环直到整数输入在要求的范围内无法处理非数字字符输入

c++ - 将字符串连接到 char*

dll - 如何将 CMFCWindowsManagerDialog 本地化为葡萄牙语?

c++ - 在 Qt5 的 QQuickItem 上捕捉 mouseMoveEvent

c++ - 如何从 C++ 的控制台应用程序显示 MFC 对话框?

c++ - 访问线程内的主对话框变量 (MFC)

C++ MFC组合框滚动条事件