GetWindowText与GetWindowTextW的区别

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”  // 需要包含此头文件

  1. void C计算器Dlg::OnCalculate()  
  2. {  
  3.     // TODO: 在此添加控件通知处理程序代码  
  4.     int a,b,c;  
  5.     TCHAR ch1[10],ch2[10],ch3[10];  //如果使用char的话,编译提示出错。msdn中给的范例采用TCHAR.  
  6.   
  7.     CString str1,str2,str3;  
  8.     GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10);  
  9.     GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,10);  
  10.     str1.Format(_T(”%s”),ch1);//把TCHAR转换为CString  
  11.     a=_wtoi(str1);// 再把CString转换为int  
  12.   
  13.     str2.Format(_T(”%s”),ch2);  
  14.     b=_wtoi(str2);  
  15.     c=a+b;  
  16.     str3.Format(_T(”%d”),c);  
  17.     GetDlgItem(IDC_AND)->SetWindowText(str3);  
  18. }  
void C计算器Dlg::OnCalculate()
{
    // TODO: 在此添加控件通知处理程序代码
    int a,b,c;
    TCHAR ch1[10],ch2[10],ch3[10];  //如果使用char的话,编译提示出错。msdn中给的范例采用TCHAR.

    CString str1,str2,str3;
    GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10);
    GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,10);
    str1.Format(_T("%s"),ch1);//把TCHAR转换为CString
    a=_wtoi(str1);// 再把CString转换为int

    str2.Format(_T("%s"),ch2);
    b=_wtoi(str2);
    c=a+b;
    str3.Format(_T("%d"),c);
    GetDlgItem(IDC_AND)->SetWindowText(str3);
}

  1. void CT3View::OnBnClickedButtonResult()  
  2. {  
  3.       
  4.     int num1,num2,num3;  
  5.     TCHAR chr1[10],chr2[10],chr3[10];     //如果定义char类的话,编译提示出错。msdn中给的范例采用TCHAR.  
  6.     CString str1,str2,str3;  
  7.     GetDlgItem(IDC_EDIT1)->GetWindowTextW(str1);  
  8.     GetDlgItem(IDC_EDIT2)->GetWindowTextW(str2);  
  9.       
  10.     num1=_wtoi(str1);// 把CString转换为int  
  11.   
  12.     num2=_wtoi(str2);  
  13.   
  14.     num3=num1+num2;  
  15.   
  16.     str3.Format(_T(”%d”),num3);  
  17.     GetDlgItem(IDC_EDIT3)->SetWindowTextW(str3);  
  18. }  
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);
}


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值