UpdateData()函数用法

 

   

 

 

    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)

   

 

 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/shuilan0066/archive/2009/10/12/4658468.aspx

相关推荐
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页
实付 39.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值