void CRegisterDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CRegisterDialog) DDX_Control(pDX, IDC_INCOME, m_ctrlIncome); DDX_LBString(pDX, IDC_INCOME, m_strIncome); DDX_CBString(pDX, IDC_KIND, m_strKind); DDX_Check(pDX, IDC_MARRIED, m_bMarried); DDX_Text(pDX, IDC_NAME, m_strName); DDX_Radio(pDX, IDC_SEX, m_nSex); DDX_Text(pDX, IDC_UNIT, m_strUnit); DDX_Radio(pDX, IDC_WORK, m_nWork); DDX_Text(pDX, IDC_AGE, m_nAge); DDV_MinMaxUInt(pDX, m_nAge, 16, 65); //}}AFX_DATA_MAP }
讀者能夠看出,該函數中的代碼是由ClassWizard自動加入的。DoDataExchange只有一個參數,即一個CDataExchange對象的指針pDX。在該函數中調用了DDX函數來完成數據交換,調用DDV函數來進行數據有效檢查。函數
當程序須要交換數據時,不要直接調用DoDataExchange函數,而應該調用CWnd::UpdateData。UpdataData函數內部調用了DoDataExchange。該函數只有一個布爾型參數,它決定了數據傳送的方向。調用UpdateData(TRUE)將數據從對話框的控件中傳送到對應的數據成員中,調用UpdateData(FALSE)則將數據從數據成員中傳送給對應的控件。ui
在缺省的CDialog::OnInitDialog中調用了UpdateData(FALSE),這樣,在對話框建立時,數據成員的初值就會反映到相應的控件上。若用戶是按了OK(肯定)按鈕退出對話框,則對話框認爲輸入有效,就會調用UpdataData(TRUE)將控件中的數據傳給數據成員。this
下圖描繪了對話框的這種數據交換機制。spa
VC中的DoDataExchange指針
怎麼設置控件和變量關聯呢?code
假如在對話框中加入一個picture控件的話對象
1.導入圖片blog
2.拉picture控件到對話框上,設置屬性,Type改成Bitmap,image選擇導入的圖片,ID必定要修改,不能爲默認的IDC_Static,不然下面將作的Classwizard將不出現此控件ID。圖片
3.ClassWizard中選擇member variable Category選擇 Control加入相應的CStatic變量 。get
4.此時到DoDataExchange看,增長相應的數據交換語句,個人是DDX_Control(pDX, IDC_bt, m_bt);
原文地址:http://hi.baidu.com/chinafire521/item/8f18a91a13edd8dbbf90424f
具體代碼以下:
文件 dlgcore.cpp
BOOL CDialog::OnInitDialog() { // execute dialog RT_DLGINIT resource BOOL bDlgInit; if (m_lpDialogInit != NULL) bDlgInit = ExecuteDlgInit(m_lpDialogInit); else bDlgInit = ExecuteDlgInit(m_lpszTemplateName); if (!bDlgInit) { TRACE(traceAppMsg, 0, "Warning: ExecuteDlgInit failed during dialog init.\n"); EndDialog(-1); return FALSE; } // transfer data into the dialog from member variables if (!UpdateData(FALSE)) { TRACE(traceAppMsg, 0, "Warning: UpdateData failed during dialog init.\n"); EndDialog(-1); return FALSE; } // enable/disable help button automatically CWnd* pHelpButton = GetDlgItem(ID_HELP); if (pHelpButton != NULL) pHelpButton->ShowWindow(AfxHelpEnabled() ? SW_SHOW : SW_HIDE); return TRUE; // set focus to first one }
wincore.cpp中
BOOL CWnd::UpdateData(BOOL bSaveAndValidate) { ASSERT(::IsWindow(m_hWnd)); // calling UpdateData before DoModal? CDataExchange dx(this, bSaveAndValidate); // prevent control notifications from being dispatched during UpdateData _AFX_THREAD_STATE* pThreadState = AfxGetThreadState(); HWND hWndOldLockout = pThreadState->m_hLockoutNotifyWindow; ASSERT(hWndOldLockout != m_hWnd); // must not recurse pThreadState->m_hLockoutNotifyWindow = m_hWnd; BOOL bOK = FALSE; // assume failure TRY { DoDataExchange(&dx); bOK = TRUE; // it worked } CATCH(CUserException, e) { // validation failed - user already alerted, fall through ASSERT(!bOK); // Note: DELETE_EXCEPTION_(e) not required } AND_CATCH_ALL(e) { // validation failed due to OOM or other resource failure e->ReportError(MB_ICONEXCLAMATION, AFX_IDP_INTERNAL_FAILURE); ASSERT(!bOK); DELETE_EXCEPTION(e); } END_CATCH_ALL pThreadState->m_hLockoutNotifyWindow = hWndOldLockout; return bOK; }