// 计数器.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;
- 1
- 2
- 3
前往页