Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 15: Fundamental Controls


CEdit m_RectHeight;
CEdit m_RectPerimeter;
CEdit m_RectArea;
};


  1. Save All


14.1.3..The Control’s Data Exchange...........................................................


After declaring the variable, you must “map” it to the control it is referring to, otherwise
the variable would behave just like any other and it cannot directly access the control you
wanted it to refer to. To specify what control your variable refers to, you must call the
DDX_Control() framework function. The syntax of this function is:

void AFXAPI DDX_Control(CDataExchange* pDX, int nIDC, CWnd& rControl);

The first argument, pDX, is a pointer to CDataExchange. The nIDC argument is the
identifier of the control your variable will refer to. The rControl argument is the name
you gave to your variable. An example of calling this function would be:

DDX_Control(pDX, IDC_STATIC_ADV, stcAdvanced);

The pDX argument in reality handles the mapping. It creates the relationship between
your rControl variable and the nIDC control that rControl must refer to. Besides that,
pDX insures that information can flow easily between both entities. The CDataExchange
class, to which the pDX argument points, is a parent-less class, meaning it is based
neither on CObject nor on CWnd.

The DDX_Control() function can be called for each variable you intend to map to a
control. When calling any of these functions, the mappings must be performed in the
CWnd::DoDataExchange() event. Its syntax is:

virtual void DoDataExchange(CDataExchange* pDX);

As you can see, this event is passed a CDataExchange pointer. This pointer in turn will
become the first argument to the DDX_Control() function. Behind the scenes, this allows
the dialog box, the parent of the controls to better manage the exchange of information
between the application and the controls.

This review helps to have an idea of how variables would be declared and associated with
the intended controls. In reality, this job is usually, and should always be, handled by
Visual C++ for you. When you create a dialog-based object, such as a dialog box, a form,
a property sheet, or a property page, the CDialog::DoDataExchange() event is created
and made ready for you. To declare a variable you want to associate to a control, unless
you have a good reason to proceed manually, use either the ClassWizard in MSVC 6 or
the Add Member Variable Wizard in MSVC 7 to add the variable. When you do this, the
wizard will take care of all the mapping for you.

After adding a variable using a wizard and once the variable mapping has been
performed, if you change the name of the variable in the header file, you must manually
change its name in the DoDataExchange() event.
Free download pdf