Two, seven ways to access the control-online lesson notes

Two, seven ways to access the control-online lesson notes

Insert picture description here

Method 1:

GetDlgItem(控件id)->GetWindowText(接收的变量,最大长度)

GetDlgItem(控件id)->SetWindowText(传的数据)

void Cmfc02demoDlg::OnBnClickedButton1()
{
	TCHAR ch1[12], ch2[12], ch3[12];//用来保存输入的字符串
	int num1, num2, num3;
	//拿到编辑框GetDlgItem(控件id)-> 获取里面的内容GetWindowTextW(接收的变量,最大长度)
	GetDlgItem(IDC_EDIT1)->GetWindowTextW(ch1,12);
	GetDlgItem(IDC_EDIT2)->GetWindowTextW(ch2, 12);
	num1 = _ttoi(ch1);
	num2= _ttoi( ch2 );
	num3 = num1 + num2;
	//将num3作为十进制数转换为字符类型赋值给ch3
	_itot_s(num3,ch3, 10);
	//修改里面的内容SetWindowTextW(传的数据)
	GetDlgItem(IDC_EDIT3)->SetWindowTextW(ch3);
}

Method 2:

GetDlgItemText(控件id,接受数据的变量,最大长度)

SetDlgItemText(控件id,传数据的变量)

void Cmfc02demoDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	TCHAR ch1[12], ch2[12], ch3[12];//用来保存输入的字符串
	int num1, num2, num3;
	//GetDlgItemText(控件id,接受数据的变量,最大长度)
	GetDlgItemText(IDC_EDIT1,ch1,12);
	GetDlgItemText(IDC_EDIT2, ch2,12);
	num3 = _ttoi(ch1) + _ttoi(ch2);
	_itot_s(num3, ch3, 10);
    //SetDlgItemText(控件id,传数据的变量)
	SetDlgItemText(IDC_EDIT3, ch3);
}

Method Three:

GetDlgItemInt(ID)Returns the data of the control obtained by id in integer

SetDlgItemInt(ID,数值)Pass integer data to the control specified by id

void Cmfc02demoDlg::OnBnClickedButton3()
{
	int num1, num2;
	num1=GetDlgItemInt(IDC_EDIT1);
	num2 = GetDlgItemInt(IDC_EDIT2);
	SetDlgItemInt(IDC_EDIT3, num1 + num2);
}

Four functions used in association:

DoDataExchange() for data association

DDX_xxxx() is associated

DDV_xxxx() for verification

UpdateData() update the associated data

UpdateData(TRUE) :将控件里的数据关联到变量里

UpdateData(FALSE):将变量的值同步到控件里

Method Four:

把控件和整形变量相关联

Add variables m_num1, m_num2, and m_num3 to the three edit boxes respectively, and add events to the buttons to get the values ​​directly in the cpp file.

Before getting the value, you need to call UpdateData(True); synchronize the value of the control to the variable; after calculation, you need to use UpdateData(FALSE); to synchronize the value of the variable into the control.

The content of the event is as follows:

void Cmfc02demoDlg::OnBnClickedButton4()
{
	UpdateData(TRUE); 
	m_num3 = m_num1 +m_num2;
	UpdateData(FALSE);
}

Method Five:

把控件和控件变量相关联

GetWindowText(接收数据的变量,数据长度最大)

SetWindowText(待传的数据)

First, add controls to the edit box separately, the control variables are named: m_edit1, m_edit2, m_edit3;

Then through the control call GetWindowText () method to get the content of the control, and then do the processing

Finally through the control call SetWindowText () method to modify the content of the control

void Cmfc02demoDlg::OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码
	int num1, num2, num3;
	TCHAR ch1[12], ch2[12], ch3[12];
	m_edit1.GetWindowText(ch1,12);
	m_edit2.GetWindowText(ch2, 12);
	num3 = _ttoi(ch2) + _ttoi(ch1);
	_itot_s(num3, ch3, 10);
	m_edit3.SetWindowText(ch3);
}

Law Six:

::SendMessage(控件的窗口句柄,消息,数据长度,数据)

LPARAM: data as an additional parameter type of the message

void Cmfc02demoDlg::OnBnClickedButton6()
{ 
	int num1,num2;
	TCHAR ch1[12], ch2[12], ch3[12];
	::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd, WM_GETTEXT, 12, (LPARAM)ch1);
	::SendMessage(GetDlgItem(IDC_EDIT2)->m_hWnd, WM_GETTEXT, 12, (LPARAM)ch2);
	_itot_s((_ttoi(ch1) + _ttoi(ch2)), ch3,10);
	::SendMessage(GetDlgItem(IDC_EDIT3)->m_hWnd, WM_SETTEXT,12, (LPARAM)ch3);
}

Method Seven:

::SendDlgItemMessage(当前窗口句柄,控件id,消息id,数据长度,数据)

Data length, data is passed as additional information of the message

void Cmfc02demoDlg::OnBnClickedButton7()
{
	int num1, num2;
	TCHAR ch1[12], ch2[12], ch3[12];
	::SendDlgItemMessage(m_hWnd, IDC_EDIT1, WM_GETTEXT, 12, (LPARAM)ch1);
	::SendDlgItemMessage(m_hWnd, IDC_EDIT2, WM_GETTEXT, 12, (LPARAM)ch2);
	_itot_s((_ttoi(ch1) + _ttoi(ch2)), ch3, 10);
	::SendDlgItemMessage(m_hWnd, IDC_EDIT3, WM_SETTEXT, 12, (LPARAM)ch3);
}

SendDlgItemMessage(控件id、消息id,数据长度,数据)

void Cmfc02demoDlg::OnBnClickedButton7()
{
	int num1, num2;
	TCHAR ch1[12], ch2[12], ch3[12];
	SendDlgItemMessage( IDC_EDIT1, WM_GETTEXT, 12, (LPARAM)ch1);
	SendDlgItemMessage( IDC_EDIT2, WM_GETTEXT, 12, (LPARAM)ch2);
	_itot_s((_ttoi(ch1) + _ttoi(ch2)), ch3, 10);
	SendDlgItemMessage(IDC_EDIT3, WM_SETTEXT, 12, (LPARAM)ch3);
}

Guess you like

Origin blog.csdn.net/weixin_49035356/article/details/109530266