MFC对话框-DoDataExchange()函数使用

来源:互联网 发布:中央网络电视台 编辑:程序博客网 时间:2024/05/16 09:20

1)CWnd::DoDataExchange 

virtualvoidDoDataExchange(CDataExchange*pDX);

Parameters

pDX

A pointer to a CDataExchange object.

Remarks

Called by the framework to exchange and validate dialog data.

Never call this function directly. It is called by the UpdateData member function. Call UpdateData to initialize a dialog box’s controls or retrieve data from a dialog box.

When you derive an application-specific dialog class from CDialog, 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 overridden DoDataExchange, 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.

The DoDataExchange overridden 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, , and in the Visual C++ Programmer's Guide. For a description of the DDX_ and DDV_ macros generated by ClassWizard, see Technical 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 值。在此情况下,对话框和对话框对象之间不进行任何数据交换。


0 0