ivy-jie

progress ...

C++博客 首页 新随笔 联系 聚合 管理
  9 Posts :: 41 Stories :: 6 Comments :: 0 Trackbacks

#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           作者:麦成勇","关于",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;  //按下了数字键
}

2 资源文件
#include "windows.h"
#include "resource.h"

/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

MENU MENU DISCARDABLE
BEGIN
    POPUP "文件(&F)"
    BEGIN
        MENUITEM "退出(&X)",                    IDM_EXIT
    END
    POPUP "编辑(&E)"
    BEGIN
        MENUITEM "复制(&C)\tCtrl+C",            IDM_COPY
        MENUITEM "粘贴(&V)\tCtrl+V",            IDM_PASTE
    END
    POPUP "帮助(&H)"
    BEGIN
        MENUITEM "关于(&A)...",                 IDM_ABOUT
    END
END


/////////////////////////////////////////////////////////////////////////////
//
// Accelerator
//

MENU ACCELERATORS MOVEABLE PURE
BEGIN
    "^A",           IDM_ABOUT,              ASCII,  NOINVERT
    "^C",           IDM_COPY,               ASCII,  NOINVERT
    "^V",           IDM_PASTE,              ASCII,  NOINVERT
END

WinIcon ICON calc.ico

posted on 2009-05-23 10:37 ivy-jie 阅读(346) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理