一段精简的MFC代码

  1. #include <afxwin.h>  // 必备的头文件,这个头文件间接包含了windows.h 
  2. class CMFCApp : public CWinApp  // 继承CWinApp 
  3.   public
  4.     virtual BOOL InitInstance();  // 虚函数 
  5.                                   // 这个函数必须重写 
  6. }; 
  7. class CMFCAppWindow : public CFrameWnd 
  8.   public
  9.     CMFCAppWindow()  // 在构造函数里创建窗体~ 
  10.     { 
  11.       Create(NULL,"KC's Windows");  // 除前两个参数外,其他参数均有初始值 
  12.     } 
  13.     // 下面是消息映射的东东 
  14.     afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);  // 左键双击的消息声明
  15.  afx_msg void OnLButtonDown(UINT nFlags,CPoint point);
  16.     afx_msg void OnPaint();  // WM_PAINT 消息声明 
  17.     DECLARE_MESSAGE_MAP()  // 消息映射宏 
  18. }; 
  19. // MS叫做消息映射表 
  20. BEGIN_MESSAGE_MAP(CMFCAppWindow, CFrameWnd) 
  21.   ON_WM_LBUTTONDBLCLK() 
  22.   ON_WM_LBUTTONDOWN()
  23.   ON_WM_PAINT() 
  24. END_MESSAGE_MAP() 
  25. // 对应的消息处理,类似SDK窗体里面的回调函数处理过程 
  26. void CMFCAppWindow::OnLButtonDblClk(UINT nFlags, CPoint point) 
  27.   MessageBox("hahahhah", NULL, MB_OK); 
  28. void CMFCAppWindow::OnLButtonDown(UINT nFlags,CPoint point)
  29. {
  30.  MessageBox("鼠标左键单击",NULL,MB_OK);
  31. }
  32. void CMFCAppWindow::OnPaint() 
  33.   CPaintDC Paint(this); 
  34.   Paint.TextOut(0, 0, "This is a sample for MFC");
  35. // 重写InitInstance虚函数 
  36. BOOL CMFCApp::InitInstance() 
  37.   m_pMainWnd = new CMFCAppWindow(); 
  38.   m_pMainWnd->ShowWindow(m_nCmdShow); 
  39.   m_pMainWnd->UpdateWindow(); 
  40.    
  41.   return TRUE; 
  42. CMFCApp theApp;   // 唯一的应用程序对象 
计算器 mfc 代码 基于VC++简易计算器实验详细过程 硬件环境: 软件环境:WinXP+VC++6.0 一、实验目的:构造一个类似Windows自带的计算器一样的简易计算器,能够连续进行加、减、乘、除四则整数运算,并能随时清除计算结果进行下一次计算。 二、具体实验步骤: 1、添加编辑框对应的变量m_Display 2、添加运算符类型变量 char CompuType; //用于存储运算符类型 CString FirstInput; //用于保存运算符前面的输入值 3、添加各种按钮的单击事件处理方法的函数 控件名称列表: 主对话框标识:IDD_CALC_DIALOG 关于对话框标识:IDD_ABOUTBOX 编辑框: IDC_EDIT 运算符按钮标识: 加: IDC_BUTTONADD 减: IDC_BUTTONSUBTRACT 乘: IDC_BUTTONMULTIPLY 除: IDC_BUTTONDIVIDE 等于: IDC_BUTTONEQUER 0~9数字符按钮标识: 7:IDC_BUTTONSEVEN 8:DC_BUTTONEIGHT 9: IDC_BUTTONNINE 4:IDC_BUTTONFOUR 5:IDC_BUTTONFIVE 6:IDC_BUTTONSIX 1:C_BUTTONONE 2:IDC_BUTTONTWO 3:IDC_BUTTONTHREE 0:IDC_BUTTONZERO 清除按钮标识:IDC_BUTTONCLEAR 关于按钮标识:IDC_BUTTONABOUT 类的成员变量: class CCalcDlg : public CDialog { // Construction public: char CompuType; //用于存储运算符类型 CCalcDlg(CWnd* pParent = NULL); // standard constructor //… (省略) protected: CString FirstInput; //用于保存运算符前面的输入值 //… (省略) }; 按钮单击的事件对应方法程序列表: void CCalcDlg::OnButtonone() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("1"); UpdateData(FALSE); } void CCalcDlg::OnButtontwo() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("2"); UpdateData(FALSE); } void CCalcDlg::OnButtonthree() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("3"); UpdateData(FALSE); } void CCalcDlg::OnButtonfour() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("4"); UpdateData(FALSE); } void CCalcDlg::OnButtonfive() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("5"); UpdateData(FALSE); } void CCalcDlg::OnButtonsix() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("6"); UpdateData(FALSE); } void CCalcDlg::OnButtonseven() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("7"); UpdateData(FALSE); } void CCalcDlg::OnButtoneight() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("8"); UpdateData(FALSE); } void CCalcDlg::OnButtonnine() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("9"); UpdateData(FALSE); } void CCalcDlg::OnButtonzero() { // TODO: Add your control notification handler code here m_Display = m_Display+_T("0"); UpdateData(FALSE); } void CCalcDlg::OnButtonadd() { // TODO: Add your control notification handler code here FirstInput = m_Display; m_Display = _T(""); UpdateData(FALSE); CompuType='+'; } void CCalcDlg::OnButtonsubtract() { // TODO: Add your control notification handler code here FirstInput = m_Display; m_Display = _T(""); UpdateData(FALSE); CompuType='-'; } void CCalcDlg::OnButtondivide() { // TODO: Add your control notification handler code here FirstInput = m_Display; m_Display = _T(""); UpdateData(FALSE); CompuType='/'; } void CCalcDlg::OnButtonmultiply() { // TODO: Add your control notification handler code here FirstInput = m_Display; m_Display = _T(""); UpdateData(FALSE); CompuType='*'; } void CCalcDlg::OnButtonequer() { // TODO: Add your control notification handler code here int TempResult; //定义存储结果的临时变量 int FirstInputValue,NewInputValue; //定义进行运算的临时变量 FirstInputValue=atoi(FirstInput.GetBuffer(24)); //将字符串变量转换成整型值 FirstInput.ReleaseBuffer(-1); NewInputValue=atoi(m_Display.GetBuffer(24)); //将字符串变量转换成整型值 m_Display.ReleaseBuffer(-1); switch(CompuType) //开关语句判断运算符类型 { case '+': TempResult = FirstInputValue + NewInputValue; //将加法运算结果传给出临时变量TempResult break; case '-': TempResult = FirstInputValue - NewInputValue; //将减法运算结果传给出临时变量TempResult break; case '*': TempResult = FirstInputValue * NewInputValue; //将乘法运算结果传给出临时变量TempResult break; case '/': TempResult = FirstInputValue / NewInputValue; //将除法运算结果传给出临时变量TempResult break; } sprintf(m_Display.GetBuffer(24),"%d",TempResult); //将运算结果变量值传递给出编辑控件变量m_Display m_Display.ReleaseBuffer(-1); UpdateData(FALSE); //刷新编辑控件变量,并在编辑框中显示运算结果 } void CCalcDlg::OnButtonclear() { // TODO: Add your control notification handler code here m_Display.Empty(); //清空存储在变量m_Display中的数据 UpdateData(FALSE); } void CCalcDlg::OnButtonabout() { // TODO: Add your control notification handler code here CAboutDlg DlgAbout; DlgAbout.DoModal();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值