Scribble must be able to retrieve the values that the user enters in the Thin Pen Width and Thick Pen Width boxes. MFC defines a mechanism that automates the process of gathering values from a dialog box; this mechanism is called a “data map.” In the same way that a message map binds a user-interface element with a member function, a data map binds a dialog-box control with a member variable. The value of the member variable reflects the status or the contents of the control. By adding entries to CPenWidthsDlg
’s data map, you can retrieve the values entered in the Thin Pen Width and Thick Pen Width boxes.
For Scribble, the widths of the thin and thick pens must be between 1 and 20. You can enforce these conditions by using the automated data validation that data maps provide. If the user enters values that fall outside this range, the application displays a message box stating the legal range and allows the user to enter new values.
To map the controls of the Pen Widths dialog box to member variables
This tab, shown in the figure below, contains a list box displaying the mapping between controls and member variables.
CPenWidthsDlg
.At the moment the box displays only the IDs for the controls because you haven’t yet specified which member variables the controls correspond to.
IDC_THIN_PEN_WIDTH
and then click the Add Variable button.The Add Member Variable dialog box appears.
m_nThinWidth
as the variable name.int
. Notice the changes in the MFC ClassWizard dialog box:
1
and 20
, respectively.IDC_THICK_PEN_WIDTH
. Specify m_nThickWidth
as the member name, click int
, and enter lower and upper limits of 1
and 20
.
You’ve now completed the data map connecting the Pen Widths dialog box to the CPenWidthsDlg
class. You can view the m_nThickWidth
and m_nThinWidth
member variables in ClassView under the CPenWidthsDlg
class.
The Member Variables Tab
ClassWizard inserts declarations into the data map of PenWidthsDlg.h for the member variables you specified in the Add Member Variable dialog box.
ClassWizard also makes changes to PenWidthsDlg.cpp after you’ve mapped the controls to member variables. Examine these changes by opening PenWidthsDlg.cpp.
Notice that ClassWizard has initialized the member variables in the constructor and provided an implementation for DoDataExchange, which is a member function defined by CWnd (the base class of CDialog). The framework calls DoDataExchange whenever values have to be moved between the member variables in the class and the controls in the dialog box on screen (for example, when first displaying the dialog box on the screen or when the user closes the dialog box by clicking OK).
The DoDataExchange function is implemented using DDX and DDV function calls. A DDX (for Dialog Data eXchange) function specifies which control in the dialog box corresponds to a particular member variable and transfers the data between the two. A DDV (for Dialog Data Validation) function specifies the validation parameters for a particular member variable, ensuring that its value is legal. The DDX and DDV function calls shown in PenWidthsDlg.cpp reflect the mapping and validation parameters you specified with ClassWizard.
Notice that the DDV function call for a given member variable immediately follows the DDX function call for that variable. This is a rule you must follow if you choose to manually edit the contents of the data map.