A dialog box is a temporary popup window an application uses to prompt the user for additional information input. A dialog box will usually contain one or more controls (child windows) with which the user can enter text, choose options, or control the direction of the application.

MFC encapsulates dialog box menus and all associated actions in the CDialog class. Dialogs are classified into modal and modeless depending on their behaviour. Modal dialog boxes prevent the user from accessing any other part of an application window until the dialog box is closed. In contrast, Modeless dialog boxes allow the user to access the application window without closing the dialog.

For simple dialogs, the CDialog class can be instantiated directly however to implement the full functionality of a dialog box it is necessary to derive a user-defined class from CDialog. This user-defined dialog class will need to have its own message map and handlers to respond to events within the dialog box since dialog box messages are not sent to the main window. A dialog box will usually be defined in a program resource file and incorporated into the application program.

A dialog box is closed when it receives an ID_CANCEL or an IDOK message. These messages are handled by the CDialog member functions OnCancel and OnOK. It is possible to override both handlers and write custom termination procedures.

Creating a Modal dialog box

To create a modal dialog box, the CDialog class constructor is initialised with details of the dialog resource identifier and the parent window. The prototype for the constructor function is

BOOL Cdialog objectname( LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL );
BOOL Cdialog objectname( UINT nIDTemplate, CWnd* pParentWnd = NULL );

where
lpszTemplateName – Contains a null-terminated string that is the name of a dialog-box template resource.
pParentWnd – Points to the parent window object (of type CWnd) to which the dialog object belongs. If it is NULL, the dialog object’s parent window is set to the main application window.
nIDTemplate – Contains the ID number of a dialog-box template resource.

returns nonzero if dialog-box creation and initialisation were successful and 0 if it fails.

Creating a modeless dialogue box

To create a modeless dialog box instantiate a CDialog class object using the default constructor without any parameters. The dialog box is then activated by a call to the CDilog member function create() instead of the doModal() member function. Modal dialog classes are usually instantiated on the stack so the dialog object won’t be destroyed prematurely when the calling procedure goes out of scope.

The following example demonstrates both a modeless and modal dialog box.

Display Code Download Code

Dialog-Based Applications

A dialog-based application is one where your main window is a dialog box. This method simplifies the application build process by making it easy to place controls onto the main window. An example of a dialog-based application is Windows calculator. In a dialog-based application, the functionality of the window will be defined in the Cdialog class instead of the CFrameWnd base. A class must be derived from the Cdialog base class and instantiated. The object reference is then stored in the MFC member variable m_pMainWnd member and the windows created by a call to the member function doModal.

The following example demonstrates how to create a simple application dialog box.

Display Code Download Code