大学期末没课,某个中午闲来无聊,正好在自学MFC,于是想用MFC、C++写点东西,由于能力有限,当然的写个简单点的啦,于是花了两个小时写了这个计算器的小程序,希望对初学VC++和MFC的朋友有所帮助。程序运行效果如下:
(1)首先按上图添加控件;
(2)然后在Dlg类的头文件中添加如下的变量,功能如注释所示:
(3)下面是程序的主要代码:
void CMyDlg::OnZero()
{
// TODO: Add your control notification handler code here
m_showend+="0";
UpdateData(false);
}
void CMyDlg::OnOne()
{
// TODO: Add your control notification handler code here
m_showend+="1";
UpdateData(false);
}
void CMyDlg::OnTwo()
{
// TODO: Add your control notification handler code here
m_showend+="2";
UpdateData(false);
}
void CMyDlg::OnThree()
{
// TODO: Add your control notification handler code here
m_showend+="3";
UpdateData(false);
}
void CMyDlg::OnFour()
{
// TODO: Add your control notification handler code here
m_showend+="4";
UpdateData(false);
}
void CMyDlg::OnFive()
{
// TODO: Add your control notification handler code here
m_showend+="5";
UpdateData(false);
}
void CMyDlg::OnSix()
{
// TODO: Add your control notification handler code here
m_showend+="6";
UpdateData(false);
}
void CMyDlg::OnSeven()
{
// TODO: Add your control notification handler code here
m_showend+="7";
UpdateData(false);
}
void CMyDlg::OnEight()
{
// TODO: Add your control notification handler code here
m_showend+="8";
UpdateData(false);
}
void CMyDlg::OnNine()
{
// TODO: Add your control notification handler code here
m_showend+="9";
UpdateData(false);
}
void CMyDlg::OnClearEditBox() //此函数用于清空编辑框信息
{
// TODO: Add your control notification handler code here
m_showend="";
operand_one=0.0;
operand_two=0.0;
UpdateData(false); //更新编辑框信息
}
void CMyDlg::OnRun()
{
// TODO: Add your control notification handler code here
operand_two=atof(m_showend);
double end=0.0;
//此处为判断操作的类型;
if(operate==add)
{
end=operand_one+operand_two;
m_showend.Format("%g",end);
}
else if(operate==subtraction)
{
end=operand_one-operand_two;
m_showend.Format("%g",end);
}
else if(operate==multiplication)
{
end=operand_one*operand_two;
m_showend.Format("%g",end);
}
else if(operate==division)
{
end=operand_one/operand_two;
m_showend.Format("%g",end);
}
UpdateData(false);
}
void CMyDlg::OnDivision() //除法运算
{
// TODO: Add your control notification handler code here
operand_one=atof(m_showend);
m_showend="";
UpdateData(false);
operate=division;//操作类型为“除”;
}
void CMyDlg::OnMultiplication() //乘法运算
{
// TODO: Add your control notification handler code here
operand_one=atof(m_showend);
m_showend="";
UpdateData(false);
operate=multiplication;//操作类型为“乘”;
}
void CMyDlg::OnSubtraction() //减法运算
{
// TODO: Add your control notification handler code here
operand_one=atof(m_showend);
m_showend="";
UpdateData(false);
operate=subtraction;//操作类型为“减”;
}
void CMyDlg::OnAdd() //加法运算
{
// TODO: Add your control notification handler code here
operand_one=atof(m_showend);
m_showend="";
UpdateData(false);
operate=add;//操作类型为“加”;
}
void CMyDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
AfxMessageBox("345234");
switch(nChar)
{
case 48:CMyDlg::OnZero();break;
case 49:CMyDlg::OnOne();break;
case 50:CMyDlg::OnTwo();break;
case 51:CMyDlg::OnThree();break;
case 52:CMyDlg::OnFour();break;
case 53:CMyDlg::OnFive();break;
case 54:CMyDlg::OnSix();break;
case 55:CMyDlg::OnSeven();break;
case 56:CMyDlg::OnEight();break;
case 57:CMyDlg::OnNine();break;
default:break;
}
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_KEYDOWN)
{
if(pMsg->wParam==VK_ESCAPE) //忽略掉键盘上的esc按键消息
{
return true;
}
//CMyDlg::OnKeyDown(nChar, nRepCnt, nFlags) ;
switch(pMsg->wParam)
{
case 48:CMyDlg::OnZero();break;
case 49:CMyDlg::OnOne();break;
case 50:CMyDlg::OnTwo();break;
case 51:CMyDlg::OnThree();break;
case 52:CMyDlg::OnFour();break;
case 53:CMyDlg::OnFive();break;
case 54:CMyDlg::OnSix();break;
case 55:CMyDlg::OnSeven();break;
case 56:CMyDlg::OnEight();break;
case 57:CMyDlg::OnNine();break;
default:break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
以上是程序的主要代码;
当然了,还得为编辑框添加一个CString类型的变量,用来保存输入的数据;下面是对话框头文件中需要定义的变量
主要的代码就这些吧,怎么添加响应函数就不用说了吧,嘿嘿