UpdateData(True): 表示写数据,将窗口控制变量写入内存(更新数据) -------使用OnOk时将自动调用UpdateData(True) ,
用以将窗口数据写入内存相应成员变量中
UpdateData(Flase): 表示读数据,即显示窗口读取内存的数据以供实时显示 ------使用OnInitDialog中,MFC调用UpdateData
(False)将内存成员数据显示到对应的窗口中
比如:CDialog的成员函数OnInitDialog()
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}
CDialog::OnOK()
void CDialog::OnOK(){ if (!UpdateData(TRUE)) { TRACE(traceAppMsg, 0, "UpdateData failed during dialog termination.\n"); // the UpdateData routine will set focus to correct item return; } EndDialog(IDOK);}
CWnd:: UpdateData(BOOL bSaveAndValidate) //UpdateData 函数的定义
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;}
所谓写数据,即是将外部数据写入到内存变量中(改变内存数据)
所谓读数据,显示窗口读取内存数据(不改变内存数据)
二者经常使用于如下情况:
在设置窗口数据默认值时,或将窗口数据作为控制参数传入到内存时,这时需要往内存写入数据,此时应首先调用UpdataData(TRUE)
写入之后,需要观察内存这个数据的变化,这时应该时刻读取内存数据,此时应调用UpdateData(false)
转载于:https://www.cnblogs.com/PierreDelatour/archive/2011/11/12/2246658.html
相关资源:JAVA上百实例源码以及开源项目