1)CWnd::DoDataExchange
virtualvoidDoDataExchange(CDataExchange*pDX);
Parameters
pDX
A pointer to aCDataExchangeobject.
Remarks
Called by the framework to exchange and validate dialog data.
Never call this function directly. It is called by theUpdateDatamember function. CallUpdateDatato initialize a dialog box’s controls or retrieve data from a dialog box.
When you derive an application-specific dialog class fromCDialog, you need to override this member function if you wish to utilize the framework’s automatic data exchange and validation. ClassWizard will write an overridden version of this member function for you containing the desired “data map” of dialog data exchange (DDX) and validation (DDV) global function calls.
To automatically generate an overridden version of this member function, first create a dialog resource with the dialog editor, then derive an application-specific dialog class. Then call ClassWizard and use it to associate variables, data, and validation ranges with various controls in the new dialog box. ClassWizard then writes the overriddenDoDataExchange, which contains a data map. The following is an example DDX/DDV code block generated by ClassWizard:
void CPenWidthsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPenWidthsDlg)
DDX_Text(pDX, IDC_THIN_PEN_WIDTH, m_nThinWidth);
DDV_MinMaxInt(pDX, m_nThinWidth, 1, 20);
DDX_Text(pDX, IDC_THICK_PEN_WIDTH, m_nThickWidth);
DDV_MinMaxInt(pDX, m_nThickWidth, 1, 20);
//}}AFX_DATA_MAP
}
ClassWizard will maintain the code within the \\{{ and \\}} delimiters. You should not modify this code.
TheDoDataExchangeoverridden member function must precede the macro statements in your source file.
For more information on dialog data exchange and validation, see in the article ODBC and MFC, , andin theVisual C++ Programmer's Guide. For a description of the DDX_ and DDV_ macros generated by ClassWizard, seeTechnical Note 26.
2)对话框数据交换
如果使用 DDX 机制,则通常在OnInitDialog处理程序或对话框构造函数中设置对话框对象成员变量的初始值。在对话框即将显示前,框架的 DDX 机制将成员变量的值传输给对话框中的控件,当对话框本身为响应DoModal或Create而出现时,这些值即出现在该对话框中。CDialog中OnInitDialog的默认实现调用CWnd类的UpdateData成员函数以初始化对话框中的控件。
当用户单击“确定”按钮(或任何时候调用参数为TRUE的UpdateData成员函数)时,同样是该机制将控件中的值传输到成员变量。对话框数据验证机制将验证您指定了验证规则的任何数据项。
下图阐释了对话框数据交换。
对话框数据交换
正如传递给它的BOOL参数所指定的那样,UpdateData进行双向交换。为了执行交换,UpdateData设置CDataExchange对象并调用对话框类对CDialog的DoDataExchange成员函数的重写。DoDataExchange采用CDataExchange类型的参数。传递给UpdateData的CDataExchange对象表示交换的上下文,它定义交换方向等信息。
当您(或某个代码向导)重写DoDataExchange时,也就指定了对每一数据成员(控件)的一个 DDX 函数的调用。UpdateData传递给您的DoDataExchange一个CDataExchange参数,每个 DDX 函数都知道如何根据该参数所提供的上下文在两个方向交换数据。
MFC 提供许多用于不同交换类型的 DDX 函数。下例显示一个DoDataExchange重写,其中调用了两个 DDX 函数和一个 DDV 函数:
void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX); // Call base class version
DDX_Check(pDX, IDC_MY_CHECKBOX, m_bVar);
DDX_Text(pDX, IDC_MY_TEXTBOX, m_strName);
DDV_MaxChars(pDX, m_strName, 20);
}
DDX_行和DDV_行是数据映射。显示的示例 DDX 和 DDV 函数分别用于复选框 (CheckBox) 控件和编辑框控件。
如果用户取消有模式对话框,则OnCancel成员函数终止该对话框,并且DoModal返回IDCANCEL值。在此情况下,对话框和对话框对象之间不进行任何数据交换。