Discussion: The About Dialog Box

A program's About dialog box displays information about the program, usually including the program's name and a copyright notice.

Figure 3.6 shows what Hello's About dialog box looks like.

To put a dialog box on the screen, OnAbout uses an object of the CModalDialog class. CModalDialog is a Microsoft Foundation class that displays and operates a modal dialog box that contains controls such as buttons and text fields. CModalDialog provides a member function, DoModal, that operates the dialog box. As long as DoModal has control, the user must deal with the dialog box. The user can use any of the controls contained in the dialog box. The user can either dismiss the dialog box, usually with a CANCEL button, or accept any changes made, usually with an OK button. Hello's dialog box has only one control, an OK button.

What OnAbout Does

The OnAbout member function constructs a CModalDialog object called “about.” The first argument to the constructor is a string that identifies a resource in an associated resource file. The second argument is this, a C++ keyword that contains a pointer to the current object. Because this code is inside a CMainWindow member function, this refers to the CMainWindow object. Passing this pointer to the dialog enables it to identify its parent window.

When to Use CModalDialog

The Microsoft Foundation's dialog box classes are derived from class CWnd, so they inherit CWnd's members and then add new functionality specific to dialogs.

The OnAbout member function of CMainWindow uses a technique suitable for very simple dialog boxes. The function simply constructs an object of class CModalDialog. (This dialog is so simple that you could implement it with a message box.) If you need a more complex dialog box, you must derive your own dialog class from CModalDialog (or from CDialog) and construct an object of the derived class. For more information on creating dialog boxes, see Chapter 5 of the tutorial and Chapter 15 of the cookbook.

Once the CModalDialog object has been constructed, OnAbout calls its DoModal member function to display the dialog and interact with the user. In Hello, the About dialog box closes when the user clicks the OK button.