mfc怎么弹出确定对话框(访问对话框控件的七种方法)
如图是一个简单的加法计算器:
方法一:
void CMy01_DemoDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
int num1, num2, num3;
TCHAR ch1[12], ch2[12], ch3[12];
GetDlgItem(IDC_EDIT1)->GetWindowText(ch1, 12);
GetDlgItem(IDC_EDIT2)->GetWindowText(ch2, 12);
num1 = _ttoi(ch1);
num2 = _ttoi(ch2);
num3 = num1 num2;
_itot(num3, ch3, 10);
GetDlgItem(IDC_EDIT3)->SetWindowText(ch3);
}
void CMy01_DemoDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
int num1, num2, num3;
TCHAR ch1[12], ch2[12], ch3[12];
GetDlgItemText(IDC_EDIT1, ch1, 12);
GetDlgItemText(IDC_EDIT2, ch2, 12);
num1 = _ttoi(ch1);
num2 = _ttoi(ch2);
num3 = num1 num2;
_itot(num3, ch3, 10);
SetDlgItemText(IDC_EDIT3, ch3);
}
void CMy01_DemoDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
int num1, num2, num3;
num1 = GetDlgItemInt(IDC_EDIT1);
num2 = GetDlgItemInt(IDC_EDIT2);
num3 = num1 num2;
SetDlgItemInt(IDC_EDIT3, num3);
}
方法四和方法五是关联法,常用到的四个函数:
DoDataExchange();
DDC_xxxx();//关联变量
DDV_xxxx();//验证变量
UpdateData();//UpdateData(TRUE)为从编辑框获取数据,UpdateData(FALSE)为更新数据到编辑框
CMy01_DemoDlg::CMy01_DemoDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_MY01_DEMO_DIALOG, pParent)
, m_num1(0)
, m_num2(0)
, m_num3(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMy01_DemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_num1);
DDX_Text(pDX, IDC_EDIT2, m_num2);
DDX_Text(pDX, IDC_EDIT3, m_num3);
DDX_Control(pDX, IDC_EDIT1, m_edit1);
DDX_Control(pDX, IDC_EDIT2, m_edit2);
DDX_Control(pDX, IDC_EDIT3, m_edit3);
}
将编辑框与整形变量关联,然后对整形成员变量直接进行操作
void CMy01_DemoDlg::OnBnClickedButton4()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
m_num3 = m_num1 m_num2;
UpdateData(false);
}
将编辑框与控件变量关联,对控件变量进行操作
void CMy01_DemoDlg::OnBnClickedButton5()
{
// TODO: 在此添加控件通知处理程序代码
int num1, num2, num3;
TCHAR ch1[12], ch2[12], ch3[12];
m_edit1.GetWindowText(ch1, 12);
m_edit2.GetWindowText(ch2, 12);
num1 = _ttoi(ch1);
num2 = _ttoi(ch2);
num3 = num1 num2;
_itot(num3, ch3, 10);
m_edit3.SetWindowText(ch3);
}
void CMy01_DemoDlg::OnBnClickedButton6()
{
// TODO: 在此添加控件通知处理程序代码
int num1, num2, num3;
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);
num1 = _ttoi(ch1);
num2 = _ttoi(ch2);
num3 = num1 num2;
_itot(num3, ch3, 10);
::SendMessage(GetDlgItem(IDC_EDIT3)->m_hWnd, WM_SETTEXT, 0, (LPARAM)ch3);
}
void CMy01_DemoDlg::OnBnClickedButton7()
{
// TODO: 在此添加控件通知处理程序代码
int num1, num2, num3;
TCHAR ch1[12], ch2[12], ch3[12];
SendDlgItemMessage(IDC_EDIT1, WM_GETTEXT, 12, (LPARAM)ch1);
SendDlgItemMessage(IDC_EDIT2, WM_GETTEXT, 12, (LPARAM)ch2);
num1 = _ttoi(ch1);
num2 = _ttoi(ch2);
num3 = num1 num2;
_itot(num3, ch3, 10);
SendDlgItemMessage(IDC_EDIT3, WM_SETTEXT, 0, (LPARAM)ch3);
}
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com