3.7 Add an About Dialog Box

This section explains the fifth step in writing Hello: add an About dialog box.

·To add an About dialog box:

1.Add the OnAbout member function to handle WM_COMMAND messages. Put the following function definition in your HELLO.CPP file with other CMainWindow member function definitions:

void CMainWindow::OnAbout()

{

CModalDialog about( "AboutBox", this );

about.DoModal();

}

As a message handler, OnAbout is similar in most respects to the OnPaint member function.

2.You already added the ON_COMMAND macro for the IDM_ABOUT menu ID to the message map. Its line in the message map looks like this:

ON_COMMAND( IDM_ABOUT, OnAbout )

The dialog template for Hello's About dialog box was created with a dialog editor. The template is in file HELLO.DLG, which is referenced in the program's resource script file, HELLO.RC, with a line like this:

rcinclude hello.dlg

This line points the resource compiler to the file containing the dialog template. Note that in the dialog template created by the dialog editor the template name “ABOUTBOX” is uppercase. However, the name is not case sensitive, as the code for OnAbout shows.

At this point, your HELLO.H and HELLO.CPP files are complete if you have followed all of the directions in this tutorial. You can check them against Listings 1 and 2, respectively.

Requirements for naming the handler function and the macro were discussed in “Rules for Message-Handler Functions” on page 98. To continue the tutorial, see “Prepare Supporting Files” on page 107.