關鍵之處在於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);
...
}