MFC Tutorial Part 5 – Dialog Boxes in MFC

Application programs in any language need some interactive screens to give inputs and obtain outputs. All windows based GUIs will definitely use dialogs for such user interactions. This part of our MFC Tutorial deals with the Dialog boxes in MFC.

Dialog Box handling is done using CDialog class in MFC. CDialog is a CWnd derived class with some extra facilities for dialog handling. The initialization of the dialog is done using an over-ridable function OnInitDialog. The dialogs are closed by calling the function EndDialog. The dialogs can accommodate a lot of controls like an Edit controls, static controls, list boxes, combo boxes, progress bars, list views, tree views and many more.

All the above mentioned controls have their own classes for handling themselves. All of them including dialog box, also have their own message handling mechanisms involving message maps. They all are treated as window/s, except with some special characteristics.

Let us see a step by step procedure for adding a dialog box to our application in this MFC Tutorial. This tutorial needs the knowledge of Creating a window MFC Tutorial part I and Menu creation as in MFC Tutorial Part IV.

Two types of dialogs can be created in MFC. They are.
1. Modal Dialogs
2. Modeless dialogs

This MFC Tutorial example demonstrates the creation of Modal dialogs.

Step1:
Create a new project of type Win32 application. In the second screen of the wizard, select the first option “Create an empty project”. Do not allow the wizard to add any files.?

Step 2:?
After the project is created, click on Menu –>Project –> Add to Project –>New and select a .cpp file and give a name?to it.

Step 3:
Copy and paste the code below.?

//MFC5.CPP - MFC Tutorial Part 5 from CoderSource.net

#include 

class MFC_Tutorial_Window :public CFrameWnd
{
    public:
    MFC_Tutorial_Window()
    {
        Create(NULL,"MFC Tutorial Part 5 CoderSource Dialog");
    }
    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP( MFC_Tutorial_Window, CFrameWnd)

END_MESSAGE_MAP()

class MyApp :public CWinApp
{
    MFC_Tutorial_Window *wnd;?
public:
    BOOL InitInstance()
    {
        wnd = new MFC_Tutorial_Window();
        m_pMainWnd = wnd;
        m_pMainWnd->ShowWindow(1);
        return 1;
    }
};

MyApp theApp;

//This code snippet has the capacity to create a window

Now we have to add the dialog resource and then add the necessary code for it

Step 4:
Click on the Menu –> Insert –> Resource –> “Dialog” and click New command button. A default dialog will be created with two command buttons.

Step 5:
On the left hand side, on the Resouces view if the Dialog tree is expanded, the resource id of the newly created dialog will be displayed. Just press Enter key on the dialog box and change the dialog id to IDR_MYDIALOG.

Step 6:
Save the resource. It will give a file name as script1.rc. Just take care that you save it to the path of your project.

Step 7:
Now click on Menu –> View –> Class Wizard or press Ctrl + W. You will be presented with a dialog asking you, if you want to create a new class. Say Yes. The wizard will create a new dialog class and related files( Newdialog.h & newdialog.cpp) for you.

Step 8:
Open the header file of your new dialog “NewDialog.h” and add the include directive for . Also add the “NewDialog.h” include directive to your project’s MFC5.cpp file that you have created in the beginning.

Step 9:
Create a menu resource and add the following code as highlighted in bold letters as follows.

//MFC5.CPP - MFC Tutorial Part 5 from CoderSource.net

#include 
#include "resource.h"
#include "newdialog.h"

class MFC_Tutorial_Window :public CFrameWnd
{
public:
    MFC_Tutorial_Window()
    {
        Create(NULL,"MFC Tutorial Part 5 CoderSource Dialog");
    }
    void OnClickDialogNew();
    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP( MFC_Tutorial_Window, CFrameWnd)
    ON_COMMAND(ID_MYDIALOG,OnClickDialogNew)
END_MESSAGE_MAP()

void MFC_Tutorial_Window::OnClickDialogNew()
{
    NewDialog dlg;
    dlg.DoModal();
}

class MyApp :public CWinApp
{
    MFC_Tutorial_Window *wnd;?
public:
    BOOL InitInstance()
    {
        wnd = new MFC_Tutorial_Window();
        m_pMainWnd = wnd;
        m_pMainWnd->ShowWindow(1);
        return 1;
    }
};

MyApp theApp;

Now, if the menu item is clicked, the dialog will be shown with the two default buttons. If either the Enter key or the escape key is pressed, the dialog will be closed. Please find the sample code here. Read here for knowing how to avoid closing the dialog when ENTER or ESCAPE key is pressed.