软件环境:Win7 64位 + VC6.0
使用MFC里的Tab控件,可以实现加减乘除。效果如下:
类型 | 成员变量 | |
IDC_EDIT_a | float | m_a |
IDC_EDIT_b | float | m_b |
IDC_EDIT_c | float | m_c |
IDC_EDIT_a2 | float | m_a2 |
IDC_EDIT_b2 | float | m_b2 |
IDC_EDIT_c2 | float | m_c2 |
IDC_EDIT_a3 | float | m_a3 |
IDC_EDIT_b3 | float | m_b3 |
IDC_EDIT_c3 | float | m_c3 |
IDC_EDIT_a4 | float | m_a4 |
IDC_EDIT_b4 | float | m_b4 |
IDC_EDIT_c4 | float | m_c4 |
ID号 | 响应函数 | |
按钮[求和] | IDC_BTN_ADD | OnBtnAdd() |
按钮[求差] | IDC_BTN_SUB | OnBtnSub() |
按钮[求积] | IDC_BTN_multi | OnBTNmulti() |
按钮[求商] | IDC_BTN_div | OnBTNdiv() |
4)为对话框关联一个类,比如,右击IDD_DIALOG_add – 》Class View –》Create class,命名为:CAddNumDlg;依次类推,IDD_DIALOG_div关联类CDivNumbDlg,IDD_DIALOG_multi关联类CMultiNumDlg、IDD_DIALOG_sub关联类CSubNumDlg。分别编写加、减、乘、除的相应函数。如下:
//相加
void CAddNumDlg::OnBtnAdd()
{
// TODO: Add your control notification handler code here
//获取值
UpdateData(TRUE);
m_c = m_a+m_b;
//显示值
UpdateData(FALSE);
}
//相减
void CSubNumDlg::OnBtnSub()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_c2 = m_a2-m_b2;
UpdateData(FALSE);
}
//相乘
void CMultiNumDlg::OnBTNmulti()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_c3 = m_a3*m_b3;
UpdateData(FALSE);
}
//相除
void CDivNumDlg::OnBTNdiv()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_b4 == 0){
MessageBox("分母不能为0!","提示",MB_ICONSTOP);
return;
}
else{
m_c4 = m_a4/m_b4;
}
UpdateData(FALSE);
}
5)初始化Tab控件,为Tab控件关联一个Control类型的变量m_Tab, 设置Tab的TCN_SELCHANGE消息。
5.1)初始化Tab控件
右击IDD_ADDSUB_DIALOG –》ClassWizard –》Member Variable –》双击IDC_TAB1 –》关联一个control类型的变量:m_Tab,如图(5)所示:
BOOL CAddSubDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_Tab.InsertItem(0,"相加",0);
m_Tab.InsertItem(1,"相减",1);
m_Tab.InsertItem(2,"相乘",2);
m_Tab.InsertItem(3,"相除",3);
m_addDlg = new CAddNumDlg;
m_subDlg = new CSubNumDlg;
m_multiDlg = new CMultiNumDlg;
m_divDlg = new CDivNumDlg;
//以Tab控件为父窗口,创建四个子对话框
m_addDlg->Create(IDD_DIALOG_add,&m_Tab);
m_subDlg->Create(IDD_DIALOG_sub,&m_Tab);
m_multiDlg->Create(IDD_DIALOG_multi,&m_Tab);
m_divDlg->Create(IDD_DIALOG_div,&m_Tab);
//获取客户区大小
CRect clientRC;
m_Tab.GetClientRect(clientRC);
//减少客户区大小
clientRC.DeflateRect(2,30,2,2);
//将对话框映射到的客户区中
m_addDlg->MoveWindow(clientRC);
m_subDlg->MoveWindow(clientRC);
m_multiDlg->MoveWindow(clientRC);
m_divDlg->MoveWindow(clientRC);
//默认为“相加”
m_addDlg->ShowWindow(SW_SHOW);
return TRUE; // return TRUE unless you set the focus to a control
}
在CAddSubDlg.h里,引用4个对话框的头文件,和4个对话框指针对象,如图(6)所示:
void CAddSubDlg::OnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
int index = m_Tab.GetCurSel();
switch(index){
case 0:
m_addDlg->ShowWindow(SW_SHOW);
m_subDlg->ShowWindow(SW_HIDE);
m_multiDlg->ShowWindow(SW_HIDE);
m_divDlg->ShowWindow(SW_HIDE);
break;
case 1:
m_addDlg->ShowWindow(SW_HIDE);
m_subDlg->ShowWindow(SW_SHOW);
m_multiDlg->ShowWindow(SW_HIDE);
m_divDlg->ShowWindow(SW_HIDE);
break;
case 2:
m_addDlg->ShowWindow(SW_HIDE);
m_subDlg->ShowWindow(SW_HIDE);
m_multiDlg->ShowWindow(SW_SHOW);
m_divDlg->ShowWindow(SW_HIDE);
break;
case 3:
m_addDlg->ShowWindow(SW_HIDE);
m_subDlg->ShowWindow(SW_HIDE);
m_multiDlg->ShowWindow(SW_HIDE);
m_divDlg->ShowWindow(SW_SHOW);
break;
}
*pResult = 0;
}
至此,用Tab控件实现加减乘除的工程,已搭建完毕,详细代码请看:
http://download.csdn.net/detail/sanqima/9299451