DDX_TEXT怎么实现数据从控件到变量还是从变量到控件

关键之处在于DDX_TEXT的第一个参数CDataExchange

CDataExchange::CDataExchange msdn:

This method constructs a CDataExchange object. CDataExchange( CWnd* pDlgWnd, BOOL bSaveAndValidate ); pDlgWnd Pointer to the parent window that contains the control. Usually this is a CDialog-derived object. bSaveAndValidate If TRUE, this object validates data and writes data from the controls to the members. If FALSE, this object moves data from members to controls.

CDataExchange类的构造函数的第二个参数bSaveAndValidate 决定了是从控件到变量(TRUE)还是从变量到控件(FALSE)之所以UpadateData(TRUE)和UpdateData(FALSE)能分别实现从控件到变量和从变量到控件,就是根据该函数传进来的TRUE或者FALSE创建不同的CDataExchange对象dx,然后再用&dx做为参数调用DoDataExchange传递给DDX_TEXT的第一个参数实现不同的数据传输。代码如下:

 BOOL CWnd::UpdateData(BOOL bSaveAndValidate)

{

 .....

CDataExchange dx(this, bSaveAndValidate);

.......

DoDataExchange(&dx);

 ....

}

void CCalenderDialog::DoDataExchange(CDataExchange* pDX)

{ .

...

DDX_Text(pDX, IDC_EDIT1, m_edit1);

 ...

}

你可能感兴趣的:(C/C++)