MFC中访问控件的几种方式

 

// DlgAddTest.cpp : implementation file
//

#include "stdafx.h"
#include "Text.h"
#include "DlgAddTest.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgAddTest dialog


CDlgAddTest::CDlgAddTest(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgAddTest::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgAddTest)
	m_iNum1 = 0;
	m_iNum2 = 0;
	m_iNum3 = 0;
	//}}AFX_DATA_INIT
}


void CDlgAddTest::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgAddTest)
	DDX_Control(pDX, IDC_EDIT3, m_Edit3);
	DDX_Control(pDX, IDC_EDIT2, m_Edit2);
	DDX_Control(pDX, IDC_EDIT1, m_Edit1);
	DDX_Text(pDX, IDC_EDIT1, m_iNum1);
	DDX_Text(pDX, IDC_EDIT2, m_iNum2);
	DDX_Text(pDX, IDC_EDIT3, m_iNum3);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgAddTest, CDialog)
	//{{AFX_MSG_MAP(CDlgAddTest)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgAddTest message handlers

void CDlgAddTest::OnButtonAdd() 
{
	//Edit3 = Edit1+Edit2

	//法1:
	/*char ch1[10],ch2[10],ch3[10];
	int num1,num2,num3;
	this->GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10);
	this->GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,10);
	num1=atoi(ch1);
	num2=atoi(ch2);
	num3=num1+num2;
	itoa(num3,ch3,10);
	this->GetDlgItem(IDC_EDIT3)->SetWindowText(ch3); */

	//法2:
	/*char ch1[10],ch2[10],ch3[10];
	int num1,num2,num3;
	GetDlgItemText(IDC_EDIT1,ch1,10);
	GetDlgItemText(IDC_EDIT2,ch2,10);
	num1=atoi(ch1);
	num2=atoi(ch2);
	num3=num1+num2;
	itoa(num3,ch3,10);
	SetDlgItemText(IDC_EDIT3,ch3);*/

	//法3:
	/*char ch1[10],ch2[10],ch3[10];
	int num1,num2,num3;
	num1 = GetDlgItemInt(IDC_EDIT1,NULL,TRUE);
	num2 = GetDlgItemInt(IDC_EDIT2,NULL,TRUE);
	num3=num1+num2;
	itoa(num3,ch3,10);
	SetDlgItemText(IDC_EDIT3,ch3);*/

	//法4: DDX1 (DoDataExchange)
	/*UpdateData();
	this->m_iNum3=this->m_iNum1 + this->m_iNum2;
	UpdateData(FALSE);*/

	//法5: DDX2
	/*char ch1[10],ch2[10],ch3[10];
	int num1,num2,num3;
	this->m_Edit1.GetWindowText(ch1,10);
	this->m_Edit2.GetWindowText(ch2,10);
	num1=atoi(ch1);
	num2=atoi(ch2);
	num3=num1+num2;
	itoa(num3,ch3,10);
	this->m_Edit3.SetWindowText(ch3);*/

	//法6:
	/*char ch1[10],ch2[10],ch3[10];
	int num1,num2,num3;
	//::SendMessage(this->GetDlgItem(IDC_EDIT1)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);
	//::SendMessage(this->m_Edit1.m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);
	//this->GetDlgItem(IDC_EDIT1)->SendMessage(WM_GETTEXT,10,(LPARAM)ch1);
	//this->m_Edit1.SendMessage(WM_GETTEXT,10,(LPARAM)ch1);
	this->SendDlgItemMessage(IDC_EDIT1,WM_GETTEXT,10,(LPARAM)ch1);
	this->m_Edit2.SendMessage(WM_GETTEXT,10,(LPARAM)ch2);
	num1=atoi(ch1);
	num2=atoi(ch2);
	num3=num1+num2;
	itoa(num3,ch3,10);
	this->m_Edit3.SendMessage(WM_SETTEXT,0,(LPARAM)(LPCTSTR)ch3);*/

//////////////////////////////////////////////////////////////////////////
	//选中部分值
	//this->m_Edit1.SendMessage(EM_SETSEL, (WPARAM) (INT)0,(LPARAM) (INT)-1);
	SendDlgItemMessage(IDC_EDIT1,EM_SETSEL, (WPARAM) (INT)0,(LPARAM) (INT)-1);
	this->m_Edit1.SetFocus();

}

posted @ 2011-02-08 02:47  庚武  Views(414)  Comments(0Edit  收藏  举报