简体   繁体   English

在MFC中显示模态对话框

[英]Display Modal dialogs in MFC

I can't find any good tutorial on MFC, I am trying to display a dialog window. 我在MFC上找不到任何好的教程,我试图显示一个对话框窗口。

I created a new resource, added my controls to it, it has inherited from CDialogEx, but I don't know where I should put the code to create and show the dialog window, I want it to be loaded when the application starts, can you give me hints? 我创建了一个新资源,向其添加了控件,它继承自CDialogEx,但是我不知道应该在哪里放置代码以创建并显示对话框窗口,我希望在应用程序启动时加载它,可以你给我提示吗?

The code should be in your application InitInstance() like so: 代码应在您的应用程序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");
    }

This will give you a dialog when you startup your application and make it the main window. 启动应用程序并将其设置为主窗口时,这将为您提供一个对话框。 This can easily be done by using your MFC Application Wizard and selecting dialog base. 通过使用MFC应用程序向导并选择对话框库,可以轻松完成此操作。 It will automatically give you the layout of your application. 它会自动为您提供应用程序的布局。

If you don't want to make it your main window just use: 如果您不想使其成为主窗口,请使用:

ExampleDlg dlg;
dlg.DoModal();

And let your dialog code do the work. 然后让您的对话代码完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM