Open the Dialog Box

You’ve specified almost everything about the Pen Widths dialog box: its appearance, the data map for its edit controls, and the message handlers for its pushbuttons. Only one thing remains to be specified: when the dialog box should be opened.

Suggested Reading in the Microsoft Foundation Class Reference

Currently no programmatic connection exists between the Pen Widths menu item and the Pen Widths dialog box. That is, the menu item and the dialog box are not bound together. You must explicitly bind them by calling the Pen Widths dialog box from within the message handler for the Pen Widths command.

How do you open a dialog box? With the following two steps:

  1. Declare a CPenWidthsDlg object. This doesn’t display the dialog box on the screen; it just constructs the C++ object that manages the dialog box.

  2. Complete the OnPenWidths member function handler for the Pen Widths menu command.

To specify that the Pen Widths command displays the dialog box modally, you call the DoModal member function defined by the CDialog class. (To display a modeless dialog box, you would call the Create member function of CDialog.)

The DoModal function continues executing as long as the dialog box is displayed on the screen. When the user clicks the OK or Cancel button, the DoModal function returns IDOK or IDCANCEL, respectively, and the application can continue.

Before you write the message handler for the Pen Widths command, you need to decide which class should get the handler. In Lesson 6, in Add New Member Variables to Scribble, you added declarations for the m_nThickWidth and m_nThinWidth member variables to the CScribbleDoc class, because the document must keep track of the widths of the thick and thin pens (this allows multiple views to share the same pen widths). Since the document class must maintain those values, it should get the handler for the Pen Widths command.