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)则将数据从数据成员中传送给对应的控件。
在缺省的CDialog::OnInitDialog中调用了UpdateData(FALSE),这样,在对话框创建时,数据成员的初值就会反映到相应的控件上。若用户是按了OK(确定)按钮退出对话框,则对话框认为输入有效,就会调用UpdataData(TRUE)将控件中的数据传给数据成员。
下图描绘了对话框的这种数据交换机制。
VC中的DoDataExchange
怎么设置控件和变量关联呢?
假如在对话框中加入一个picture控件的话
1.导入图片
2.拉picture控件到对话框上,设置属性,Type改为Bitmap,image选择导入的图片,ID一定要修改,不能为默认的IDC_Static,否则下面将做的Classwizard将不出现此控件ID。
3.ClassWizard中选择member variable Category选择 Control加入相应的CStatic变量 。
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; }