A dialog box is a temporary popup window used by an application 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 two types depending on their behaviour: modal and modeless. Modal dialog boxes don’t allow the user to access 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 itself.
For the simple dialogs, the CDialog class can be instantiated directly however in order 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 maps and handlers to react 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 however possible to override both of these handlers and write custom termination procedures.
Creating a Modal dialog box
To create a modal dialog box, the CDialog class constructor will need to be 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 first, 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() as opposed to the doModal() member function. Modal dialog classes are usually instantiated on the stack so that the dialog object won’t be destroyed prematurely when the calling procedure goes out of scope.
The code section below demonstrates both a modeless and modal dialog box.
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 easier to place controls onto the main window. An example of a dilog bases 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 therefore must be derived from the Cdialog base class and instantiated. The object reference is then stored in the MFC smember variable m_pMainWnd member and then the windows created by a call to the member function doModal.
The code section below demonstrates how to create simple application dialog box.