简单计算器设计(一)
要求:设计一个对话框应用程序,在对话框模板上添加控件,实现简单计算功能
1
修改MFC
AppWizard自动创建的对话框项目模板。删除模板上自动创建的三个控件。加入“简单计算器”、“数一”、“数二”、“结果”四个静态文本框。
继续用鼠标从工具箱中向对话框添加3个编辑框和6个按钮控件,更改按钮的标题。
2
用ClassWizard为编辑框添加变量:m_Num1,
m_Num2,
m_Num3
3
用ClassWizard为按钮控件添加消息映射:
4
在实现文件(cpp文件)中加入单击按钮时的事件响应代码。(双击一个按钮可直接进入函数编辑)
void
CJsqDlg::OnButton1()
{
UpdateData(TRUE);
m_Num3=m_Num1+m_Num2;
UpdateData(FALSE);
}
void
CJsqDlg::OnButton2()
{
UpdateData(TRUE);
m_Num3=m_Num1-m_Num2;
UpdateData(FALSE);
}
void
CJsqDlg::OnButton3()
{
m_Num1=0;
m_Num2=0;
m_Num3=0;
UpdateData(FALSE);
MessageBox("欢迎重新开始计算,张志明
20040910");
}
void
CJsqDlg::OnButton4()
{
UpdateData(TRUE);
m_Num3=m_Num1m_Num2;
UpdateData(FALSE);
}
void
CJsqDlg::OnButton5()
{
UpdateData(TRUE);
if(m_Num2==0)
MessageBox("除数不能为0,张志明
20040910");
else
m_Num3=m_Num1/m_Num2;
UpdateData(FALSE);
}
void
CJsqDlg::OnButton6()
{
CDialog::OnOK();
}
3
简单计算器(二)
运行时界面如下:
编程提示:
1
添加变量列表:
变量类型
名称
float
m_N1
float
m_N2
float
m_N3
char
fuhao
2
添加成员变量fuhao的方法:
在WorkSpace中进入ClassView视图,
右键单击CxxxDlg,
在菜单中选择“Add
Member
Variable…”,然后在d出的对话框的输入变量类型和名称。
3添加的代码:
void
CJsq2Dlg::
OnRadio1Plus()
//缺省选择加法
{
fuhao='+';
}
void
CJsq2Dlg::OnRadio2()
{
fuhao='-';
}
void
CJsq2Dlg::OnRadio3()
{
fuhao='';
}
void
CJsq2Dlg::OnRadio4()
{
fuhao='/';
}
//计算
void
CJsq2Dlg::OnButton1()
{
UpdateData(TRUE);
switch(fuhao)
{
case
'+':
m_N3=m_N1+m_N2;break;
case
'-':
m_N3=m_N1-m_N2;
break;
case
'':
m_N3=m_N1m_N2;
break;
case
'/':
if(m_N2
=
=
0
)
MessageBox("除数不能为零!");
else
{
m_N3
=
m_N1/m_N2;
break;
}
default:
m_N3=m_N1+m_N2;
}
UpdateData(FALSE);
}
//清除
void
CJsq2Dlg::OnClear()
{
UpdateData(TRUE);
m_N1=0;
m_N2=0;
m_N3=0;
UpdateData(FALSE);
}
//关闭
void
CJsq2Dlg::OnOK()
{
CDialog::OnOK();
}
2楼的方法,可以改这个可执行文件的图标,2+3楼的方法可以改运行后主窗口左上角的图标跟任务栏的图标总起来就是:
在资源(Resource)窗口下,有一个Icon文件夹,IDR_MAINFRAME就是应用程序的图标,请根据需要选择格式一般是标准(3232),然后把你需要的图标拷进去,也可以自己手工描绘,这样,主图标(也就是编译生成的文件图标)就变成你想要的了
如果是基于对话框的程序,还可以继续替换掉主界面跟标题栏的图标
在WM_INITDIALOG中写
hInstance=::GetModuleHandle(NULL);
SendMessage(hwnd,WM_SETICON,ICON_BIG,
LPARAM(LoadIcon ( hInstance,MAKEINTRESOURCE (IDR_MAINFRAME) ) ) );
MFC深入浅出
demo:
头文件:
// CalculatorDlgh : 头文件#pragma once// CCalculatorDlg 对话框class CCalculatorDlg : public CDialogEx{// 构造public:CCalculatorDlg(CWnd pParent = NULL); // 标准构造函数void SaveValue1();// 对话框数据enum { IDD = IDD_CALCULATOR_DIALOG };protected:virtual void DoDataExchange(CDataExchange pDX); // DDX/DDV 支持// 实现protected:HICON m_hIcon;// 生成的消息映射函数virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();DECLARE_MESSAGE_MAP()public:afx_msg void OnBnClickedButton0();afx_msg void OnBnClickedButton1();afx_msg void OnBnClickedButton2();afx_msg void OnBnClickedButton3();afx_msg void OnBnClickedButton4();afx_msg void OnBnClickedButton5();afx_msg void OnBnClickedButton6();afx_msg void OnBnClickedButton7();afx_msg void OnBnClickedButton8();afx_msg void OnBnClickedButton9();afx_msg void OnBnClickedPoint();afx_msg void OnBnClickedButtonJia();afx_msg void OnBnClickedButtonJian();afx_msg void OnBnClickedButtonCheng();afx_msg void OnBnClickedButtonChu();afx_msg void OnBnClickedButtonAc();afx_msg void OnBnClickedButtonGenhao();afx_msg void OnBnClickedButtonCifang();afx_msg void OnBnClickedButtonBack();afx_msg void OnBnClickedButtondDeng();afx_msg void OnEnChangeEdit1(); double m_fNumber1; double m_fNumber2;int m_iSign; //计算 void Calculator(); void CCalculatorDlg::OnButtonKaiFang() ;void CCalculatorDlg::OnButtonPingFang();void CCalculatorDlg::OnButtonClear();CString m_str;afx_msg void OnBnClickedButton10();afx_msg void OnBnClickedButton12();afx_msg void OnBnClickedButton11();afx_msg void OnBnClickedButton13();};源文件:
// CalculatorDlgcpp : 实现文件#include "stdafxh"#include "Calculatorh"#include "CalculatorDlgh"#include "afxdialogexh"#include <stdlibh>#include <mathh>#include<stdioh>#ifdef _DEBUG#define new DEBUG_NEW#endif// 用于应用程序“关于”菜单项的 CAboutDlg 对话框class CAboutDlg : public CDialogEx{public:CAboutDlg();// 对话框数据enum { IDD = IDD_ABOUTBOX };protected:virtual void DoDataExchange(CDataExchange pDX); // DDX/DDV 支持// 实现protected:DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD){}void CAboutDlg::DoDataExchange(CDataExchange pDX){CDialogEx::DoDataExchange(pDX);} BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)END_MESSAGE_MAP() // CCalculatorDlg 对话框CCalculatorDlg::CCalculatorDlg(CWnd pParent /=NULL/): CDialogEx(CCalculatorDlg::IDD, pParent), m_str(_T("")){m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CCalculatorDlg::DoDataExchange(CDataExchange pDX){CDialogEx::DoDataExchange(pDX);DDX_Text(pDX, IDC_EDIT1, m_str);DDX_Text(pDX, IDC_EDIT1, m_str);}BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON_0, &CCalculatorDlg::OnBnClickedButton0)ON_BN_CLICKED(IDC_BUTTON_6, &CCalculatorDlg::OnBnClickedButton6)ON_BN_CLICKED(IDC_BUTTON_1, &CCalculatorDlg::OnBnClickedButton1)ON_BN_CLICKED(IDC_BUTTON_7, &CCalculatorDlg::OnBnClickedButton7)ON_BN_CLICKED(IDC_BUTTON_JIA, &CCalculatorDlg::OnBnClickedButtonJia)ON_BN_CLICKED(IDC_BUTTON_AC, &CCalculatorDlg::OnBnClickedButtonAc)ON_BN_CLICKED(IDC_BUTTON_GENHAO, &CCalculatorDlg::OnBnClickedButtonGenhao)ON_BN_CLICKED(IDC_BUTTON_CIFANG, &CCalculatorDlg::OnBnClickedButtonCifang)ON_BN_CLICKED(IDC_BUTTON_CHU, &CCalculatorDlg::OnBnClickedButtonChu)ON_BN_CLICKED(IDC_BUTTON_BAIFEN, &CCalculatorDlg::OnBnClickedButtonBack)ON_BN_CLICKED(IDC_BUTTOND_DENG, &CCalculatorDlg::OnBnClickedButtondDeng)ON_BN_CLICKED(IDC_POINT, &CCalculatorDlg::OnBnClickedPoint)ON_BN_CLICKED(IDC_BUTTON_JIAN, &CCalculatorDlg::OnBnClickedButtonJian)ON_BN_CLICKED(IDC_BUTTON_8, &CCalculatorDlg::OnBnClickedButton8)ON_BN_CLICKED(IDC_BUTTON_9, &CCalculatorDlg::OnBnClickedButton9)ON_BN_CLICKED(IDC_BUTTON_CHENG, &CCalculatorDlg::OnBnClickedButtonCheng)ON_EN_CHANGE(IDC_EDIT1, &CCalculatorDlg::OnEnChangeEdit1)ON_BN_CLICKED(IDC_BUTTON_10, &CCalculatorDlg::OnBnClickedButton10)ON_BN_CLICKED(IDC_BUTTON_12, &CCalculatorDlg::OnBnClickedButton12)ON_BN_CLICKED(IDC_BUTTON_11, &CCalculatorDlg::OnBnClickedButton11)ON_BN_CLICKED(IDC_BUTTON_13, &CCalculatorDlg::OnBnClickedButton13)END_MESSAGE_MAP()// CCalculatorDlg 消息处理程序BOOL CCalculatorDlg::OnInitDialog(){CDialogEx::OnInitDialog(); // 将“关于”菜单项添加到系统菜单中。// IDM_ABOUTBOX 必须在系统命令范围内。ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){BOOL bNameValid;CString strAboutMenu;bNameValid = strAboutMenuLoadString(IDS_ABOUTBOX);ASSERT(bNameValid);if (!strAboutMenuIsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}} // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动// 执行此 *** 作SetIcon(m_hIcon, TRUE); // 设置大图标SetIcon(m_hIcon, FALSE); // 设置小图标// TODO: 在此添加额外的初始化代码return TRUE; // 除非将焦点设置到控件,否则返回 TRUE}void CCalculatorDlg::OnSysCommand(UINT nID, LPARAM lParam){if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAboutDoModal();}else{CDialogEx::OnSysCommand(nID, lParam);}} // 如果向对话框添加最小化按钮,则需要下面的代码// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,// 这将由框架自动完成。 \\void CCalculatorDlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // 用于绘制的设备上下文SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dcGetSafeHdc()), 0);// 使图标在工作区矩形中居中int cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rectWidth() - cxIcon + 1) / 2;int y = (rectHeight() - cyIcon + 1) / 2;// 绘制图标dcDrawIcon(x, y, m_hIcon);}else{CDialogEx::OnPaint();}} //当用户拖动最小化窗口时系统调用此函数取得光标//显示。HCURSOR CCalculatorDlg::OnQueryDragIcon(){return static_cast<HCURSOR>(m_hIcon);}void CCalculatorDlg::OnBnClickedButton2(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); m_str = m_str + '2'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButton3(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); m_str = m_str + '3'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButton5(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); m_str = m_str + '5'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButton0(){// TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); m_str = m_str + '0'; UpdateData(FALSE); }void CCalculatorDlg::OnBnClickedButton6(){// TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); m_str = m_str + '6'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButton1(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); m_str = m_str + '1'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButton7(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); m_str= m_str + '7'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButtonJia(){// TODO: 在此添加控件通知处理程序代码SaveValue1(); m_iSign = 0; }void CCalculatorDlg::OnBnClickedButtonAc(){// TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); m_str = ""; m_fNumber1 = 00; m_fNumber2 = 00; m_iSign = 0; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButtonGenhao(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); m_fNumber1 = _ttof (m_str); double f = sqrt(m_fNumber1); //如果浮点数其实是个整数 if (f - int(f) <= 1e-5){m_strFormat("%d", (int)f);}else{m_strFormat("%f", f);} UpdateData(FALSE); m_fNumber1 = f; m_fNumber2 = 00; m_iSign = 5; }void CCalculatorDlg::OnBnClickedButtonCifang(){// TODO: 在此添加控件通知处理程序代码 // TODO: Add your control notification handler code here UpdateData(TRUE); m_fNumber1 = _ttof (m_str); double f = m_fNumber1 m_fNumber1; //如果浮点数其实是个整数 if (f - int(f) <= 1e-5){m_strFormat("%d", (int)f);}else{m_strFormat("%f", f);} UpdateData(FALSE); m_fNumber1 = f; m_fNumber2 = 00; m_iSign = 4; }void CCalculatorDlg::OnBnClickedButtonChu(){// TODO: 在此添加控件通知处理程序代码SaveValue1(); m_iSign = 3; }void CCalculatorDlg::OnBnClickedButtonBaifen(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); //移除最右边一个字符 if (!m_strIsEmpty()) { m_str = m_strLeft(m_strGetLength()-1); } UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButtondDeng(){// TODO: 在此添加控件通知处理程序代码Calculator(); }void CCalculatorDlg::OnBnClickedPoint(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE); //如果没有小数点,则加上一个小数点,如果已有小数点就忽略此次的小数点,保证最多只有1个 if (-1 == m_strFind('')) { m_str = m_str + ''; } UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButtonJian(){// TODO: 在此添加控件通知处理程序代码SaveValue1(); m_iSign = 1; }void CCalculatorDlg::OnBnClickedButton8(){// TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); m_str = m_str + '8'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButton9(){// TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); m_str = m_str + '9'; UpdateData(FALSE);}void CCalculatorDlg::OnBnClickedButtonCheng(){// TODO: 在此添加控件通知处理程序代码SaveValue1(); m_iSign = 2;}void CCalculatorDlg::SaveValue1() { UpdateData(TRUE); m_fNumber1 = _ttof (m_str); m_str = ""; UpdateData(FALSE); } void CCalculatorDlg::Calculator(){ UpdateData(TRUE);m_fNumber2 = _ttof(m_str);double f = 00;switch (m_iSign){//加 case 0:f = m_fNumber1 + m_fNumber2;break;//减 case 1:f = m_fNumber1 - m_fNumber2;break;//乘 case 2:f = m_fNumber1 m_fNumber2;break;//除 case 3:if (m_fNumber2 == 00){f = m_fNumber1;}else{f = m_fNumber1 / m_fNumber2;}break;//平方 default:break;}//如果浮点数其实是个整数 if (f - int(f) <= 1e-5){m_strFormat("%d", (int)f);}else{m_strFormat("%f", f);}UpdateData(FALSE); m_fNumber1 = f;m_fNumber2 = 00;} void CCalculatorDlg::OnEnChangeEdit1(){// TODO: 如果该控件是 RICHEDIT 控件,它将不// 发送此通知,除非重写 CDialogEx::OnInitDialog()// 函数并调用 CRichEditCtrl()SetEventMask(),// 同时将 ENM_CHANGE 标志“或”运算到掩码中。// TODO: 在此添加控件通知处理程序代码}#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"请输入要求和的两个数:"<<endl;
cin>>a>>b;
cout<<"计算结果为:"<<a+b<<endl;
return 0;
}
以上就是关于c语言的问题,能帮我写个在VC6里可以运行的计算器全部的内容,包括:c语言的问题,能帮我写个在VC6里可以运行的计算器、我用VC环境编了个计算器程序(用的是C语言),我想要更换可执行文件的图标,不怎么要怎么更换.能帮帮我嘛、Vc++6.0 mfc做一个简单计算器 能加减乘除开方平方就可以, 最主要的是工程名改为p0206等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)