Dialog Boxes
A dialog box is a temporary pop-up window that an application uses to prompt the user for additional information or input. A dialog box usually contains one or more controls (child windows) that allow the user to enter text, choose options, or control the operation of the application.
MFC encapsulates dialog box functionality in the CDialog class. Dialog boxes are classified as either modal or modeless, depending on their behaviour. A modal dialog box prevents the user from interacting with the rest of the application until the dialog box is closed. In contrast, a modeless dialog box allows the user to continue working with the application while the dialog remains open.
Although it is possible to instantiate the CDialog class directly for very simple dialogs, most applications derive their own dialog class from CDialog (or CDialogEx in newer versions of MFC). The derived class contains its own message map and message handlers because messages generated by controls within the dialog are sent to the dialog itself rather than the application’s main window.
Dialog boxes are normally defined as resources using the Visual Studio resource editor and are compiled into the application’s executable. A dialog box is typically closed when it receives an IDOK or IDCANCEL command. These commands are processed by the virtual member functions OnOK() and OnCancel(), both of which can be overridden to perform custom validation or cleanup before the dialog closes.
Creating a Modal Dialog Box
A modal dialog box is created by constructing a dialog object using the dialog resource identifier and then calling the member function DoModal(). The constructors are:
CDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL);
CDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL);
where
- lpszTemplateName – Specifies the name of the dialog-box template resource.
- nIDTemplate – Specifies the resource identifier of the dialog-box template.
- pParentWnd – Points to the parent window. If
NULL, the application’s main window is used.
The DoModal() member function creates the dialog, displays it, enters its own message loop, and does not return until the dialog is closed.
Its return value is typically:
- IDOK – The user accepted the dialog.
- IDCANCEL – The user cancelled or closed the dialog.
- -1 – The dialog could not be created.
Because DoModal() does not return until the dialog is closed, modal dialog objects are usually created as local (stack) variables.
Creating a Modeless Dialog Box
A modeless dialog box is created using the default constructor followed by a call to the Create() member function.
BOOL Create(UINT nIDTemplate, CWnd* pParentWnd = NULL);
BOOL Create(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL);
where
- nIDTemplate – Specifies the dialog resource identifier.
- lpszTemplateName – Specifies the name of the dialog template.
- pParentWnd – Points to the parent window.
Create() returns a nonzero value if the dialog is successfully created; otherwise it returns 0.
Unlike a modal dialog, Create() returns immediately after the dialog is displayed, allowing the user to continue interacting with the rest of the application. Because the creating function may return while the dialog is still visible, modeless dialogs are normally allocated dynamically using new. They must also be explicitly destroyed when they are no longer required, typically by calling DestroyWindow(), after which the dialog object is usually deleted.
The code section below demonstrates a modeless and modal dialog box.
Dialog-Based Applications
A dialog-based application is an application whose main window is a dialog box rather than a frame window. This approach simplifies application development because controls such as buttons, edit boxes, list boxes, and combo boxes can be placed directly onto the main window using the Visual Studio resource editor. A familiar example of a dialog-based application is the Windows Calculator.
In an MFC dialog-based application, the application’s main window is represented by a class derived from the CDialog class (or CDialogEx in newer versions of MFC). The derived class contains the dialog’s controls, message map, and event handlers. During application initialisation, an instance of this dialog class is created and assigned to the MFC application member variable m_pMainWnd. The dialog is then displayed by calling the DoModal() member function.
Unlike an SDI or MDI application, a dialog-based application does not normally contain a frame window, document, or view architecture. Instead, the dialog itself provides the primary user interface and remains active until it is closed. When the dialog is dismissed by the user, DoModal() returns and the application normally terminates.