【问题标题】:Display Modal dialogs in MFC在 MFC 中显示模态对话框
【发布时间】:2015-09-09 02:55:22
【问题描述】:

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

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

【问题讨论】:

  • 这里似乎是一个合理的开始:codeproject.com/KB/MFC
  • 您希望对话框成为主窗口吗?然后在创建项目时,选择“基于对话框”作为应用程序类型。

标签: c++ mfc


【解决方案1】:

代码应该在您的应用程序 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();

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-20
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 2013-09-22
    • 2017-10-14
    • 1970-01-01
    相关资源
    最近更新 更多