简体   繁体   中英

Display Modal dialogs in MFC

I can't find any good tutorial on MFC, I am trying to display a dialog window.

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?

The code should be in your application InitInstance() like so:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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