UpdateData(TRUE);//將控件上顯示上的數據更新到關聯變量
UpdateData(FALSE);將關聯變量的值更新到控件顯示。
當你改變控件對應的關聯變量的值的時候,要使用UpdateData(FALSE)來更新顯示。
當你在界面上更改控件的值的時候,你要使用UpdateData(TRUE);將值更新到關聯變量
或者如果不使用關聯變量,可以使用GetDlgItemText(IDC_EDIT,str);或SetDlgItemText(IDC_EDIT,str)來更改控件的值的時候,就不需要UpdateData()函數
msdn中也確是這么定義的:
int GetWindowText( LPTSTR lpszStringBuf, int nMaxCount ) const;
void GetWindowTextW( CString& rString ) const;
要實現一個計算加法的功能。三個文本編輯框,分別為IDC_EDIT1 , IDC_EDIT2 , IDC_EDIT3, 一個button控件,點擊后可以將IDC_EDIT1中的輸入數字加上IDC_EDIT2中的輸入數字的結果顯示到IDC_EDIT3中。
GetWindowText和GetWindowTextW方法
#include "Tchar.h" // 需要包含此頭文件
void CT3View::OnBnClickedButtonResult() { int num1,num2,num3; TCHAR chr1[10],chr2[10],chr3[10]; //如果定義char類的話,編譯提示出錯。msdn中給的范例采用TCHAR. CString str1,str2,str3; GetDlgItem(IDC_EDIT1)->GetWindowText(chr1,10); GetDlgItem(IDC_EDIT2)->GetWindowText(chr2,10); str1.Format(_T("%s"),chr1);//把TCHAR轉換為CString num1=_wtoi(str1);// 再把CString轉換為int str2.Format(_T("%s"),chr2); num2=_wtoi(str2); num3=num1+num2; str3.Format(_T("%d"),num3); GetDlgItem(IDC_EDIT3)->SetWindowText(str3); }
void CT3View::OnBnClickedButtonResult() { int num1,num2,num3; TCHAR chr1[10],chr2[10],chr3[10]; //如果定義char類的話,編譯提示出錯。msdn中給的范例采用TCHAR. CString str1,str2,str3; GetDlgItem(IDC_EDIT1)->GetWindowTextW(str1); GetDlgItem(IDC_EDIT2)->GetWindowTextW(str2); num1=_wtoi(str1);// 把CString轉換為int num2=_wtoi(str2); num3=num1+num2; str3.Format(_T("%d"),num3); GetDlgItem(IDC_EDIT3)->SetWindowTextW(str3); }
