MFC对话框控件访问的七种方式

void CTestDlg::OnButtonAdd() 
{
	// TODO: Add your control notification handler code here
	//动态创建按钮
/*	if ( FALSE == m_bIsCreate )  // 亦可以通过静态变量 static BOOL bIsCreate = FALSE;
	{
		m_btn.Create("Lee", BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD, CRect(0,0,100,65), this, 111);
	//	m_btn.ShowWindow(SW_NORMAL);//若上句没有WS_VISIBLE,就加上showWindow
		m_bIsCreate = TRUE;
	}
	else
	{
		m_btn.DestroyWindow();
		m_bIsCreate = FALSE;
	}
*/
/*	//最简单的动态创建按钮
	if (!m_btn.m_hWnd) //窗口句柄不为空
	{
		m_btn.Create("Lee", BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD, CRect(0,0,100,65), this, 111);
	}
	else
	{
		m_btn.DestroyWindow(); // 会自动将 m_hWnd = NULL
	}
*/
	//实现 点击Add按钮,将 编辑框1的数 + 编辑框2 的数  放到编辑框3中
/*	int num1, num2, num3;
	char ch1[10], ch2[10], ch3[10];
	GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10); //将编辑框的文本存到ch1中,最大10个字符
	GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,10);
	
	num1 = atoi(ch1); // 将字符串转换为整型
	num2 = atoi(ch2);
	num3 = num1 + num2;
	itoa(num3, ch3, 10); // 将整数num3 转换到ch3中,以十进制的方式
	
	GetDlgItem(IDC_EDIT3)->SetWindowText(ch3); // 将ch3字符数值,存到编辑框3中
*/
/*	//也可以
	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);
*/
/*	//还可以
	int num1, num2, num3;
	num1 = GetDlgItemInt(IDC_EDIT1); // 直接获得编辑框中的数值
	num2 = GetDlgItemInt(IDC_EDIT2);

	num3 = num1 + num2;

	SetDlgItemInt(IDC_EDIT3, num3);
*/
/*	//最简单的一种,用classwizard 关联 控件与成员变量(以值)
	UpdateData();
	m_num3 = m_num2 + m_num1;
	UpdateData(FALSE);
*/
/*	//第五种方式  用classwizard 关联 控件与成员变量(以控件)。与第一种相似
	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);
*/
/*	//第六种方式,发送消息
	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, 10, (LPARAM)ch3);
*/
	//第七种方式
	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, 10, (LPARAM)ch3);
	SendDlgItemMessage(IDC_EDIT3, EM_SETSEL, 0, -1); //复选所有的文本
	//GetDlgItem(IDC_EDIT3)->SetFocus(); //将焦点聚到edit3. 或者 
	m_edit3.SetFocus();
}

  

posted @ 2018-07-21 13:45  htj10  阅读(284)  评论(0编辑  收藏  举报
TOP