Your browser doesn't support JavaScript Dialog Windows - Windows Programming

Dialog Windows

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 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(). 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 code section below demonstrates a modeless and modal dialog box.

Download Code

Dialog-Based Applications

A dialog-based application is where the main window is a dialog box. This method simplifies the application build process by making controls easier to place onto the main window. An example of a dialogue-based application is the Windows calculator. Windows functionality will be defined in the Cdialog class in a dialog-based application. A class must therefore 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 window created by a call to the member function doModal.

Download Code