Implementing the Message Handler

Recall that ClassWizard provided an empty function definition for the OnDefaultPenWidths message handler, which is called when the user clicks the Default button. Now that the CPenWidthsDlg class contains the necessary member variables, m_nThickWidth and m_nThinWidth, it’s time to fill in that function definition.

Suggested Reading in the Microsoft Foundation Class Reference

The OnDefaultPenWidths function sets the contents of the edit boxes to the default widths of the thin and thick pens.

To implement the message handler for the Default button

  1. Use WizardBar to jump to the OnDefaultPenWidths function definition in PenWidthsDlg.cpp, and add the following code (delete the //TODO comments):
    m_nThinWidth = 2;
    m_nThickWidth = 5;
    UpdateData(FALSE);  // causes DoDataExchange() 
    // bSave=FALSE means don't save from screen, rather, write
    // to screen
    
  2. Save your changes to PenWidthsDlg.cpp.

OnDefaultPenWidths sets m_nThinWidth and m_nThickWidth to their default values and then calls UpdateData, a member function defined by CWnd (the base class of CDialog).

The UpdateData member function calls the DoDataExchange function to move values between the member variables and the controls displayed on the screen. The direction in which the data values are moved is specified by the argument to UpdateData. The default value of this argument is TRUE, which moves data from the controls to the member variables. A value of FALSE moves data from the member variables to the controls. The OnDefaultPenWidths member function passes FALSE, causing the default values to be displayed in the edit boxes on the screen.