計算器制作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://www.rrbay.com