MFC入门示例之访问对话框控件的7种方法

方法一:

 1 //方法一 
 2 void CMFCApplication2Dlg::OnBnClickedButton1()
 3 {
 4     int num1, num2, num3;
 5     TCHAR ch1[12], ch2[12], ch3[12];
 6     //获取文本值
 7     GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,12);
 8     GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,12);
 9     //字符串转为数字
10     num1 = _ttoi(ch1);
11     num2 = _ttoi(ch2);
12     num3 = num1 + num2;
13     
14     _itot(num3, ch3, 10);//数字转化为字符串(10进制)
15     GetDlgItem(IDC_EDIT3)->SetWindowTextW(ch3);
16 }

方法二:

 1 //方法二 
 2 void CMFCApplication2Dlg::OnBnClickedButton1()
 3 {
 4     int num1, num2, num3;
 5     TCHAR ch1[12], ch2[12], ch3[12];
 6     //获取文本值
 7     GetDlgItemText(IDC_EDIT1, ch1, 12);
 8     GetDlgItemText(IDC_EDIT2, ch2, 12);
 9     //字符串转为数字
10     num1 = _ttoi(ch1);
11     num2 = _ttoi(ch2);
12     num3 = num1 + num2;
13     
14     _itot(num3, ch3, 10);//数字转化为字符串(10进制)
15     SetDlgItemText(IDC_EDIT3, ch3);
16 }

方法三:

 1 //方法三 
 2 void CMFCApplication2Dlg::OnBnClickedButton1()
 3 {
 4     int num1, num2, num3;
 5     //获取文本值
 6     num1 = GetDlgItemInt(IDC_EDIT1);
 7     num2 = GetDlgItemInt(IDC_EDIT2);
 8     num3 = num1 + num2;
 9     
10     SetDlgItemInt(IDC_EDIT3,num3);
11 }

方法四:

1 //方法四 控件和整型变量相关联
2 void CMFCApplication2Dlg::OnBnClickedButton1()
3 {
4     //同步控件到变量
5     UpdateData(TRUE);
6     m_num3 = m_num2 + m_num1;
7     //同步变量到控件
8     UpdateData(FALSE);
9 }

方法五:

 1 //方法五 控件和控件变量相关联
 2 void CMFCApplication2Dlg::OnBnClickedButton1()
 3 {
 4     int num1, num2, num3;
 5     TCHAR ch1[12], ch2[12], ch3[12];
 6     //获取值
 7     m_edit1.GetWindowText(ch1,12);
 8     m_edit2.GetWindowText(ch2,12);
 9     //字符串转为数字
10     num1 = _ttoi(ch1);
11     num2 = _ttoi(ch2);
12     num3 = num1 + num2;
13     _itot(num3, ch3, 10);//数字转化为字符串(10进制)
14 
15     m_edit3.SetWindowText(ch3);
16 }

方法六:

 1 //方法六 Windows API SendMessage
 2 void CMFCApplication2Dlg::OnBnClickedButton1()
 3 {
 4     int num1, num2, num3;
 5     TCHAR ch1[12], ch2[12], ch3[12];
 6     
 7     ::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd,WM_GETTEXT,12,(LPARAM)ch1);
 8     ::SendMessage(GetDlgItem(IDC_EDIT2)->m_hWnd,WM_GETTEXT,12,(LPARAM)ch2);
 9      
10     num1 = _ttoi(ch1);
11     num2 = _ttoi(ch2);
12     num3 = num1 + num2;
13     _itot(num3, ch3, 10);//数字转化为字符串(10进制)
14 
15     ::SendMessage(GetDlgItem(IDC_EDIT3)->m_hWnd,WM_SETTEXT,0,(LPARAM)ch3);
16 }

方法七:

 1 //方法七 Windows API SendDlgItemMessage
 2 void CMFCApplication2Dlg::OnBnClickedButton1()
 3 {
 4     int num1, num2, num3;
 5     TCHAR ch1[12], ch2[12], ch3[12];
 6 
 7     SendDlgItemMessage(IDC_EDIT1, WM_GETTEXT, 12, (LPARAM)ch1);
 8     SendDlgItemMessage(IDC_EDIT2, WM_GETTEXT, 12, (LPARAM)ch2);
 9 
10     num1 = _ttoi(ch1);
11     num2 = _ttoi(ch2);
12     num3 = num1 + num2;
13     _itot(num3, ch3, 10);//数字转化为字符串(10进制)
14 
15     SendDlgItemMessage(IDC_EDIT3, WM_SETTEXT, 0, (LPARAM)ch3);
16 }
posted @ 2018-06-21 15:55  量化未来  阅读(251)  评论(0编辑  收藏  举报