获取控件上的文本值---例子是CEdit 的七种方法

获取控件上的文本值---例子是CEdit 的七种方法
 

获取控件上的文本值---例子是CEdit 的七种方法_第1张图片

1.       
int num1,num2,num3;
char ch1[10],ch2[10],ch3[10];

GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10);
GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,10);

num1=atoi(ch1);
num2=atoi(ch2);
num3=num1+num2;
itoa(num3,ch3,10);

GetDlgItem(IDC_EDIT3)->SetWindowText(ch3);
2.
int num1,num2,num3;
char ch1[10],ch2[10],ch3[10];

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.
int num1,num2,num3;
num1=GetDlgItemInt(IDC_EDIT1);
num2=GetDlgItemInt(IDC_EDIT2);

num3=num1+num2;
SetDlgItemInt(IDC_EDIT3,num3);
4.//定义成员变量..变量与控件关连
/**
//{{AFX_DATA(CTestDlg)
enum { IDD = IDD_DIALOG1 };
int   m_num1;
int   m_num2;
int   m_num3;
//}}AFX_DATA
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
DDX_Text(pDX, IDC_EDIT1, m_num1);
DDV_MinMaxInt(pDX, m_num1, 0, 100);
DDX_Text(pDX, IDC_EDIT2, m_num2);
DDV_MinMaxInt(pDX, m_num2, 0, 100);
DDX_Text(pDX, IDC_EDIT3, m_num3);
//}}AFX_DATA_MAP
}
**/
UpdateData();
m_num3=m_num1+m_num2;
UpdateData(FALSE);
/*
CWnd::UpdateData
BOOL UpdateData( BOOL bSaveAndValidate = TRUE );
Return Value
Nonzero if the operation is successful; otherwise 0. If bSaveAndValidate is TRUE, then a return value of nonzero means that the data is successfully validated.
Parameters
bSaveAndValidate
Flag that indicates whether dialog box is being initialized (FALSE) or data is being retrieved (TRUE).
Remarks
Call this member function to initialize data in a dialog box, or to retrieve and validate dialog data.
The framework automatically calls UpdateData with bSaveAndValidate set to FALSE when a modal dialog box is created in the default implementation of CDialog::OnInitDialog. The call occurs before the dialog box is visible. The default implementation of CDialog::OnOK calls this member function with bSaveAndValidate set to TRUE to retrieve the data, and if successful, will close the dialog box. (If the Cancel button is clicked in the dialog box, the dialog box is closed without the data being retrieved.)
*/
5.//定义控件变量..变量与控件关连
//{{AFX_DATA(CTestDlg)
enum { IDD = IDD_DIALOG1 };
CEdit m_edit3;
CEdit m_edit2;
CEdit m_edit1;
//}}AFX_DATA
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
DDX_Control(pDX, IDC_EDIT3, m_edit3);
DDX_Control(pDX, IDC_EDIT2, m_edit2);
DDX_Control(pDX, IDC_EDIT1, m_edit1);
//}}AFX_DATA_MAP
}

int num1,num2,num3;
char ch1[10],ch2[10],ch3[10];

m_edit1.GetWindowText(ch1,10);
m_edit2.GetWindowText(ch2,10);

num1=atoi(ch1);
num2=atoi(ch2);
num3=num1+num2;
itoa(num3,ch3,10);

m_edit3.SetWindowText(ch3);
6.

int num1,num2,num3;
char ch1[10],ch2[10],ch3[10];

//::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);//方法一
//::SendMessage(m_edit1.m_hWnd,WM_GETTEXT,10,(LPARAM)ch1);//方法二
//GetDlgItem(IDC_EDIT1)->SendMessage(WM_GETTEXT,10,(LPARAM)ch1);//方法三

m_edit1.SendMessage(WM_GETTEXT,10,(LPARAM)ch1);//方法四
m_edit2.SendMessage(WM_GETTEXT,10,(LPARAM)ch2);

num1=atoi(ch1);
num2=atoi(ch2);
num3=num1+num2;
itoa(num3,ch3,10);

m_edit3.SendMessage(WM_SETTEXT,0,(LPARAM)ch3);
7.
int num1,num2,num3;
char ch1[10],ch2[10],ch3[10];

SendDlgItemMessage(IDC_EDIT1,WM_GETTEXT,10,(LPARAM)ch1);
SendDlgItemMessage(IDC_EDIT2,WM_GETTEXT,10,(LPARAM)ch2);

num1=atoi(ch1);
num2=atoi(ch2);
num3=num1+num2;
itoa(num3,ch3,10);

SendDlgItemMessage(IDC_EDIT3,WM_SETTEXT,0,(LPARAM)ch3);
SendDlgItemMessage(IDC_EDIT3,EM_SETSEL,0,-1);

你可能感兴趣的:(获取控件上的文本值---例子是CEdit 的七种方法)