zoukankan      html  css  js  c++  java
  • 视频教程:计算器制作MFC

    计算器制作MFC

     

    最近,很多同学都问我系统自带的计算器用C++怎么写,要求用MFC做,我现在就给大家制作个简单的计算器。严格的说,我并不是MFC工程,而是Windows API编程,而不是用MFC应用框架做。具体的制作过程我已经做成视频了,请你自己详细的看。

     

     

    大家看完视频发现,我的"计算器.cpp""resource.h"怎么写的?我相信大家也没有兴趣看我一个一个的代码敲进工程里面吧!至于那个两个文件怎么写,如何写,每句代码什么意思什么意思? 请看详细的代码和说明。

     

    计算器.cpp :

     

    // 计数器.cpp : Defines the entry point for the application.

    //

     

    #include "stdafx.h"

    #include<windows.h>

    #include <math.h>

    #include <stdio.h>

    #include <stdlib.h>

    #include<string.h>

    #include"resource.h"

     

    HWND hWndhWnd,

         hEditResult,

         hButtonOptSum0,hButtonOptSum1,hButtonOptSum2,hButtonOptSum3,

            hButtonOptSum4,hButtonOptSum5,hButtonOptSum6,hButtonOptSum7,

            hButtonOptSum8,hButtonOptSum9,hButtonOptSumDec,

         hButtonOptAdd,hButtonOptSub,hButtonOptMul,hButtonOptDiv,

            hButtonOptSqrt,

            hButtonOptPercent,hButtonOptEqu,

            hButtonCancelEntry;

            

    HINSTANCE hInst;

    char lpszAddItem[20]="";

    char lpszResult[20]="";

    char lpszResult1[20]="";

    char lpszResult2[20]="";

    char lpszOpt[]="N";  //贮存操作符号

    char *stop;

    double nAddItem=0,nResult=0;

    double nResult1=0,nResult2=0;

    int nMax;

    int nOptF=0;  //判断是否按了操作符键

    bool bDec=false;    //判断是否按了点操作符;

     

    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

    void EquResult();               //按下操作符(+,-,*,/,sqrt,%,=)处理函数

    void NumResult(char *NumData);  //按下数字键(0~9和小数点)的操作处理函数

     

     

     

     

     

    //-------------------------主函数------------------------

    int WINAPI WinMain(HINSTANCE hInstance,

                         HINSTANCE hPrevInstance,

                         LPSTR     lpCmdLine,

                         int       nCmdShow)

    {

          // TODO: Place code here.

           HWND hWnd;

        MSG Message;

           WNDCLASS WndClass;

           char lpszClassName[]="编辑框";

           char lpszTitle[]="计算器";

           WndClass.cbClsExtra=0;

           WndClass.cbWndExtra=0;

           WndClass.hbrBackground=(HBRUSH)(GetStockObject(LTGRAY_BRUSH)); //设置窗体背景:亮灰色

        WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);

           //WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);

           WndClass.hIcon=LoadIcon(NULL,"WinIcon");

           WndClass.hInstance=hInstance;

           WndClass.lpfnWndProc=WndProc;    //消息处理

           WndClass.lpszClassName=lpszClassName;

        WndClass.lpszMenuName="Menu";    //加载菜单

           WndClass.style=0;

          

        if(!RegisterClass(&WndClass))

           {

                  MessageBeep(0);

                  return FALSE;

           }

     

       hInst=hInstance;

       hWnd=CreateWindow(

                                 lpszClassName,

                                 lpszTitle,

                                          WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX,

                                          CW_USEDEFAULT,

                                          CW_USEDEFAULT,

                                          210,230,

                                          NULL,

                                          NULL,

                                          hInstance,

                                          NULL

                                         );

        ShowWindow(hWnd,nCmdShow);

         UpdateWindow(hWnd);

          

           while(GetMessage(&Message,NULL,0,0))

           {

                      TranslateMessage(&Message);

                      DispatchMessage(&Message);

           }

           return Message.wParam;

    }

     

     

    //------------------------消息处理--------------------------------

    LRESULT CALLBACK WndProc(HWND hwnd,

                                               UINT message,

                                               WPARAM wParam,

                                               LPARAM lParam)

    {

           switch(message)

           {

           case WM_CREATE:

            hEditResult=CreateWindow("EDIT",       //建立文本框

                                             NULL,

                                                            WS_CHILD | WS_VISIBLE | ES_RIGHT | WS_BORDER | ES_READONLY,

                                                            10,10,

                                                            185,24,

                                                            hwnd,

                                                            (HMENU)IDE_RESULT,

                                                            hInst,

                                                            NULL);

                  hButtonOptSum7=CreateWindow("BUTTON",  //建立按钮7

                                             "7",

                                                            WS_CHILD | WS_VISIBLE,

                                                            10,40,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU) IDB_NUM7,

                                                            hInst,

                                                            NULL);

                  hButtonOptSum8=CreateWindow("BUTTON",  //建立按钮8

                                             "8",

                                                            WS_CHILD | WS_VISIBLE,

                                                            45,40,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU) IDB_NUM8,

                                                            hInst,

                                                            NULL);

                  hButtonOptSum9=CreateWindow("BUTTON",   //建立按钮9

                                             "9",

                                                            WS_CHILD | WS_VISIBLE,

                                                            80,40,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU) IDB_NUM9,

                                                            hInst,

                                                            NULL);

                  hButtonOptSum4=CreateWindow("BUTTON",   //建立按钮4

                                             "4",

                                                            WS_CHILD | WS_VISIBLE,

                                                            10,75,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU) IDB_NUM4,

                                                            hInst,

                                                            NULL);

                  hButtonOptSum5=CreateWindow("BUTTON",   //建立按钮5

                                             "5",

                                                            WS_CHILD | WS_VISIBLE,

                                                            45,75,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU) IDB_NUM5,

                                                            hInst,

                                                            NULL);

                  hButtonOptSum6=CreateWindow("BUTTON",   //建立按钮6

                                             "6",

                                                            WS_CHILD | WS_VISIBLE,

                                                            80,75,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU) IDB_NUM6,

                                                            hInst,

                                                            NULL);

                  hButtonOptSum1=CreateWindow("BUTTON",    //建立按钮1

                                             "1",

                                                            WS_CHILD | WS_VISIBLE,

                                                            10,110,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU) IDB_NUM1,

                                                            hInst,

                                                            NULL);

                  hButtonOptSum2=CreateWindow("BUTTON",   //建立按钮2

                                             "2",

                                                            WS_CHILD | WS_VISIBLE,

                                                            45,110,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU) IDB_NUM2,

                                                            hInst,

                                                            NULL);

                  hButtonOptSum3=CreateWindow("BUTTON",   //建立按钮3

                                             "3",

                                                            WS_CHILD | WS_VISIBLE,

                                                            80,110,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU) IDB_NUM3,

                                                            hInst,

                                                            NULL);

                  hButtonOptSum0=CreateWindow("BUTTON",    //建立按钮0

                                             "0",

                                                            WS_CHILD | WS_VISIBLE,

                                                            10,145,

                                                            65,30,

                                                            hwnd,

                                                            (HMENU) IDB_NUM0,

                                                            hInst,

                                                            NULL);

                  hButtonOptSumDec=CreateWindow("BUTTON",  //建立按钮.

                                             ".",

                                                            WS_CHILD | WS_VISIBLE,

                                                            80,145,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU) IDB_NUMDEC,

                                                            hInst,

                                                            NULL);

               hButtonOptSqrt=CreateWindow("BUTTON",  //建立按钮Sqr

                                             "Sqr",

                                                            WS_CHILD | WS_VISIBLE,

                                                            130,40,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU) IDB_OPTSQRT,

                                                            hInst,

                                                            NULL);

                  hButtonCancelEntry=CreateWindow("BUTTON",  //建立按钮CE

                                             "C",

                                                            WS_CHILD | WS_VISIBLE,

                                                            165,40,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU) IDB_CANCLEENTRY,

                                                            hInst,

                                                            NULL);

                  hButtonOptAdd=CreateWindow("BUTTON",    //建立按钮+

                                             "+",

                                                            WS_CHILD | WS_VISIBLE,

                                                            130,75,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU)IDB_OPTADD,

                                                            hInst,

                                                            NULL);

                  hButtonOptSub=CreateWindow("BUTTON",  //建立按钮-

                                             "-",

                                                            WS_CHILD | WS_VISIBLE,

                                                            165,75,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU)IDB_OPTSUB,

                                                            hInst,

                                                            NULL);

                  hButtonOptMul=CreateWindow("BUTTON",   //建立按钮*

                                             "*",

                                                            WS_CHILD | WS_VISIBLE,

                                                            130,110,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU)IDB_OPTMUL,

                                                            hInst,

                                                            NULL);

                  hButtonOptDiv=CreateWindow("BUTTON",   //建立按钮/

                                             "/",

                                                            WS_CHILD | WS_VISIBLE,

                                                            165,110,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU)IDB_OPTDIV,

                                                            hInst,

                                                            NULL);

                  hButtonOptEqu=CreateWindow("BUTTON",  //建立按钮=

                                             "=",

                                                            WS_CHILD | WS_VISIBLE,

                                                            130,145,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU)IDB_OPTEQU,

                                                            hInst,

                                                            NULL);

                  hButtonOptPercent=CreateWindow("BUTTON",   //建立按钮%

                                             "%",

                                                            WS_CHILD | WS_VISIBLE,

                                                            165,145,

                                                            30,30,

                                                            hwnd,

                                                            (HMENU)IDB_OPTPERCENT,

                                                            hInst,

                                                            NULL);

          

                  SetWindowText(hEditResult,"0");

                  break;

           case WM_SETFOCUS:

                  SetFocus(hEditResult);

                  break;

          

           case WM_COMMAND:

              switch(LOWORD(wParam))

              {

                      // 零至玖与点按钮

                         case IDB_NUM0:

                                          if (nOptF==0)

                                                 break;

                                           NumResult("0");

                                              break;

                    case IDB_NUM1:

                                          NumResult("1");

                                              break;

                                  case IDB_NUM2:

                                           NumResult("2");                                    

                                           break;

                      case IDB_NUM3:

                                           NumResult("3");                                    

                                           break;

                      case IDB_NUM4:

                                           NumResult("4");

                                           break;

                      case IDB_NUM5:

                                           NumResult("5");

                                           break;

                      case IDB_NUM6:

                                           NumResult("6");

                                           break;

                      case IDB_NUM7:

                                           NumResult("7");

                                           break;

                               case IDB_NUM8:

                                           NumResult("8");

                                           break;

                      case IDB_NUM9:

                                           NumResult("9");

                                           break;

                      case IDB_NUMDEC:

                                              if (bDec==true)

                                                     break;  //如果已按了点号就返回

                            NumResult(".");

                            nOptF=1;      //按了操作符键

                                              bDec=true;    //按了点操作符;

                                              break;

                     

                                        //MessageBox(hwnd,"没有此功能!","功能",MB_OK);

                                        break;

                         // ,,,除与百分数按钮                    

                            case IDB_OPTADD:

                            EquResult();

                                              strcpy(lpszOpt,"+");     //设置按了操作符号+

                                           break;

                                  case IDB_OPTSUB:                                                                     

                                           EquResult();                                     

                                              strcpy(lpszOpt,"-");

                                           break;

                                  case IDB_OPTMUL:

                                              EquResult();

                                              strcpy(lpszOpt,"*");     //设置按了操作符号+

                                           break;

                                  case IDB_OPTDIV: 

                                         //算出结果

                                           EquResult();                              

                                              strcpy(lpszOpt,"/");

                                           break;

                                  case IDB_OPTPERCENT: 

                                         //算出结果

                                           EquResult();                              

                                              strcpy(lpszOpt,"%");

                                        break;

                                // 等于按钮    

                                  case IDB_OPTEQU:

                                           //算出等于结果

                                           EquResult();                                     

                                              strcpy(lpszOpt,"N");

                                           break;

                             //开平方按钮   

                                  case IDB_OPTSQRT:

                                         EquResult();                                  

                                         strcpy(lpszOpt,"S");

                                      break;

                                  case IDB_CANCLEENTRY:

                                         SetWindowText(hEditResult,"0");

                                      nResult=0;

                                      nAddItem=0;

                                         nResult1=0;

                                         nResult2=0;

                                         strcpy(lpszResult1,"0");

                          strcpy(lpszResult2,"0");

                                         nOptF=0;

                                         bDec=false;

                                         strcpy(lpszOpt,"N");  //贮存操作符号

                                         break;

                         //关于菜单

                          case IDM_ABOUT:                

                     MessageBox(hwnd,"欢迎使用本记算器!\n\n\n           作者:Gemgin","关于",MB_OK|MB_ICONINFORMATION);

                                 break;

                          case IDM_COPY:

                                 break;

                         case IDM_PASTE:

                                 break;

                //退出菜单

                          case IDM_EXIT:                

                     SendMessage(hwnd,WM_DESTROY,0,0L);

                                 break;

                        

     

                                 

              }

              break;

              case WM_DESTROY:

                      PostQuitMessage(0);

                      break;

               default:

                      return DefWindowProc(hwnd,message,wParam,lParam);

           }

           return 0;

    }

     

     

     

    //-----------------------按下操作符(+,-,*,/,sqrt,%,=)处理函数-----------------------

    void EquResult()

    {

          

     //算出结果

                                           if (strcmp(lpszOpt,"N")==0)

                                              {

                                                     nResult1=strtod(lpszResult1,&stop);                                             

                                              }

                                              else

                                              {

                                                     switch(*lpszOpt)    //比较上一次按的操作符后所得的结果

                                                     {

                                                     case '+':

                                                            nResult1=strtod(lpszResult1,&stop);

                                                            nResult2=strtod(lpszResult2,&stop);

                                                            nResult1=nResult1+nResult2;

                                                            break;

                                                     case '-':

                                                            nResult1=strtod(lpszResult1,&stop);

                                                            nResult2=strtod(lpszResult2,&stop);

                                                            nResult1=nResult1-nResult2;

                                                            break;

                                                     case '*':

                                                            nResult1=strtod(lpszResult1,&stop);

                                                            nResult2=strtod(lpszResult2,&stop);

                                                            nResult1=nResult1*nResult2;

                                                            break;

                                                     case '/':

                                                            nResult1=strtod(lpszResult1,&stop);

                                                            nResult2=strtod(lpszResult2,&stop);

                                                            if (nResult2==0)

                                                            {

                                                               MessageBox(hWndhWnd,"除数不能为零!","功能",MB_OK);

                                                               break;

                                                            }

                                                            nResult1=nResult1/nResult2;

                                                            break;

                                                     case '%':

                                                            nResult1=strtod(lpszResult1,&stop);

                                                            nResult1=nResult1/100;

                                                            break;

                                                     case 'S':

                                                            nResult1=strtod(lpszResult1,&stop);

                                                            if (nResult1<0)

                                                            {

                                                                   MessageBox(hWndhWnd,"负数没有平方根!","没意义",MB_OK);

                                                                break;

                                                            }

                                                            nResult=sqrt( nResult1 );

                                                            nResult1=nResult;

                                                            break;

                                                     }

                                                           

                                              }

                            nResult1=nResult1*1.0;

                                           _gcvt(nResult1,15,lpszResult1);    //双精度转化为字符串                             

                                           SetWindowText(hEditResult,lpszResult1);

                                              nOptF=0;      

                                              bDec=false;

    }

     

     

     

    //--------------------------按下数字键(0~9和小数点)的操作处理函数--------------------

    void NumResult(char *NumData)

    {

          

                                           if (nOptF==0)

                                                  SetWindowText(hEditResult,"");   

                                           nMax=GetWindowTextLength(hEditResult)+1;

                                           GetWindowText(hEditResult,lpszAddItem,nMax);

                                           strcat(lpszAddItem,NumData);        ///字符串加该数字键的字符

                                           if (strcmp(lpszOpt,"N")==0)

                                              {

                                                     strcpy(lpszResult1,lpszAddItem);

                                               SetWindowText(hEditResult,lpszResult1);

                                              }

                                              else

                                              {

                                                     strcpy(lpszResult2,lpszAddItem);

                                                SetWindowText(hEditResult,lpszResult2);

                                              }

                            nOptF=1;  //按下了数字键

    }

     

    resource.h源代码:

     

    //{{NO_DEPENDENCIES}}

    // Microsoft Developer Studio generated include file.

    // Used by menu.rc

    //

    #define IDM_CLOSE                       12

    #define IDM_EXIT                        17

    #define IDM_COPY                        19

    #define IDM_PASTE                       20

    #define IDM_HELP                        22

    #define IDM_ABOUT                       27

    #define IDE_RESULT                      101

    #define IDB_NUM0                        110

    #define IDB_NUM1                        111

    #define IDB_NUM2                        112

    #define IDB_NUM3                        113

    #define IDB_NUM4                        114

    #define IDB_NUM5                        115

    #define IDB_NUM6                        116

    #define IDB_NUM7                        117

    #define IDB_NUM8                        118

    #define IDB_NUM9                        119

    #define IDB_NUMDEC                      120

    #define IDB_OPTADD                      121

    #define IDB_OPTSUB                      122

    #define IDB_OPTMUL                      123

    #define IDB_OPTDIV                      124

    #define IDB_OPTSQRT                     125

    #define IDB_OPTPERCENT                  126

    #define IDB_OPTEQU                      127

    #define IDB_CANCLEENTRY                 129

     

    // Next default values for new objects

    //

    #ifdef APSTUDIO_INVOKED

    #ifndef APSTUDIO_READONLY_SYMBOLS

    #define _APS_NEXT_RESOURCE_VALUE        104

    #define _APS_NEXT_COMMAND_VALUE         40001

    #define _APS_NEXT_CONTROL_VALUE         1000

    #define _APS_NEXT_SYMED_VALUE           101

    #endif

    #endif

     

    计算器MFC制作视频下载:

     

    ftp://ctfysj.kongjian.in:******@ctfysj.kongjian.in/视频教学文件/计算器MFC.rar

     

    ******是密码,为了隐私,勿诉于人,如你需要,请回复

    密码:

     

    **** 本内容跟帖回复才可浏览 *****

     

    文件名:  计算器MFC.rar
    下载地址: 

    http://cachefile5.fs2you.com/zh-cn/download/2b044f2b29e5774330f2aa29e8f7fc83/%E8%AE%A1%E7%AE%97%E5%99%A8MFC.rar

    更多内容请关注太阳湾官方网站 :http://www.rrbay.com

  • 相关阅读:
    JVM堆内存设置和测试
    转:面试题:“你能不能谈谈,java GC是在什么时候,对什么东西,做了什么事情?”
    JVM--标记-清除算法Mark-Sweep
    JVM 垃圾回收器工作原理及使用实例介绍
    BSGS算法(大小步算法)
    Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
    Codeforces Round #605 (Div. 3)
    POJ 2516Minimum Cost(最小费用流+特判)
    POJ 3155Hard Life(最大密度子图)
    洛谷P2463 [SDOI2008]Sandy的卡片(后缀数组SA + 差分 + 二分答案)
  • 原文地址:https://www.cnblogs.com/Gemgin/p/3136465.html
Copyright © 2011-2022 走看看