Declaring the CPenWidthsDlg Object

In the following procedure you’ll use WizardBar to add a function handler for the OnPenWidths message and bind the function to its handler code, which is executed whenever the user clicks the Pen Widths command.

To declare the CPenWidthsDlg object

  1. Open ScribbleDoc.cpp in the text editor.

  2. Click the arrow on the action button on the right end of WizardBar.

  3. Click Add Windows Message Handler.

    The New Windows Message Handler dialog box appears.

  4. In the Class or object to handle list box, select ID_PEN_WIDTHS.

  5. In the New Windows messages to handle list box, select COMMAND.

  6. Click Add and Edit.

  7. In the Add Member Function dialog box, click OK to accept the candidate name “OnPenWidths.”

  8. In place of the highlighted //TODO comment, add the following code:
    CPenWidthsDlg dlg;
    // Initialize dialog data
    dlg.m_nThinWidth = m_nThinWidth;
    dlg.m_nThickWidth = m_nThickWidth;
    
    // Invoke the dialog box
    if (dlg.DoModal() == IDOK)
    {
    // retrieve the dialog data
    m_nThinWidth = dlg.m_nThinWidth;
    m_nThickWidth = dlg.m_nThickWidth;
    
    // Update the pen used by views when drawing new strokes
    // to reflect the new pen widths for "thick" and "thin".
    ReplacePen();
    }
    
  9. Scroll to the top of ScribbleDoc.cpp and add the following #include statement:
    #include "PenWidthsDlg.h"
    
  10. Save ScribbleDoc.cpp.

    When you modify ScribbleDoc.cpp, you must include PenWidthsDlg.h so the message handler has access to the dialog class you’ve created.

The OnPenWidths function:

  1. Declares a CPenWidthsDlg object and sets the values of the m_nThickWidth and m_nThinWidth member variables to the current widths of the thick and thin pens.

  2. Calls the DoModal function, which displays the dialog box on the screen and takes control of the application until the user exits the dialog box. If the user exits the dialog box by clicking the OK button, the function changes the current thick and thin pen widths to the new values; if the user clicks the Cancel button, the old values are retained.

  3. Calls the ReplacePen member function to make the document’s pen use the current widths.

When does the application perform the data exchange and validation defined in the DoDataExchange function? Recall that DoDataExchange is called by the UpdateData member function. Just before the dialog box is first displayed on the screen, the framework calls the UpdateData function with an argument of FALSE, which sets the contents of the edit boxes to the values of the member variables. If the user exits the dialog box by clicking the OK button, the framework calls UpdateData with an argument of TRUE, which retrieves the contents of the edit boxes and sets the values of the member variables accordingly. (If the user exits by clicking the Cancel button, the framework doesn’t call UpdateData.)

You don’t have to handle the UPDATE_COMMAND_UI message for the Pen Widths menu item because the menu item doesn’t need to be updated. The command is never disabled since it’s always legal to change the widths of the pens, and there’s no need to add or remove a check mark because the command isn’t a toggle.