电脑上的【计算器】程序升级到Win10后不能用了,好像是使用了应用商店技术。又不想安装应用商店,再者使用了应用商店技术,就不好做成绿色的(复制到U盘上就可以运行)。于是就用VC++(Visual Studio 2017)做了一个。
VC不熟,难免有不足之处。
软件下载地址:http://yongcun.cc/shop/jsq/jsq.exe
源码下载地址:http://yongcun.cc/shop/jsq/jsq.zip
#include <windows.h>
#include <stdlib.h>
#include <tchar.h>
#include <string>
using namespace std;
static TCHAR szWindowClass[] = _T("DesktopApp");
static TCHAR szTitle[] = _T("简易绿色计算器");
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void putForm(HWND hWnd);
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = 0;// CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(101));//IDI_APPLICATION
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(101));//IDI_APPLICATION
if (!RegisterClassEx(&wcex)){
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
hInst = hInstance;
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_CAPTION | WS_SYSMENU,//WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
200, 290,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd){
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
putForm(hWnd);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
double calc(double a, char o, double b) {
if (o == 11) {//+
return a + b;
}
else if (o == 12) {//-
return a - b;
}
else if (o == 13) {//*
return a * b;
}
else if (o == 14) {// /
return a / b;
}
else return 0;
}
double v0 = 0, v1 = 0;
string v2 = "0";
char op0 = 11;
bool number = false;
bool dot = false;
HWND textBox;
void formatV2() {
if(v2.length() > 1 && v2.substr(0, 1).compare("0") == 0)v2 = v2.substr(1, v2.length() - 1);//去除前导0
if(!dot)if (v2.length() >= 1 && v2.substr(v2.length() - 1, 1).compare(".")==0)v2 = v2.substr(0, v2.length() - 1);//去除尾部.
}
void show(double v) {
char s[40];
string v2;
_gcvt_s(s, 40, v, 20);
v2 = string(s);
if (v2.length() >= 1 && v2.substr(v2.length() - 1, 1).compare(".")==0)v2 = v2.substr(0, v2.length() - 1);
SetWindowTextA(textBox, (LPCSTR)&v2);
}
void show(string v) {
if (v.length() > 1 && v.substr(0,1).compare("0")==0)v = v.substr(1, v.length() - 1);
SetWindowTextA(textBox, (LPCSTR)&v);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
int n;
switch (message){
case WM_PAINT:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
n = wParam - 1000;
switch (n) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
if (number) {
formatV2();
v2 = v2 + (char)(48 + n);
v1 = atof(v2.c_str());
}else{
char buf[30];
v1 = n;
_gcvt_s(buf,20,v1,10);
v2 = string(buf);
formatV2();
number = true;
dot = false;
}
formatV2();
show(v2);
break;
case 10://.
if (!dot)v2 = v2 + ".";
dot = true;
show(v2);
break;
case 15://=
if(number)v1 = atof(v2.c_str());
v0 = calc(v0, op0, v1);
show(v0);
number = false;
break;
case 11:
case 12:
case 13:
case 14://+-*/
v1 = atof(v2.c_str());
if (number)v0=calc(v0, op0, v1);
op0 = n;
v1 = 0;
number = false;
show(v0);
break;
case 16://AC
v0 = 0;
op0 = 11;//+
v2 = "0";
show(v2);
number = true;
dot = 0;
break;
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
HFONT hf;
int n = 0, H = 40, W=40;
void putControl(HWND hWnd, LPCWSTR t, int w,int id) {
HWND hBtn;
int i=0,row,col;
row = n / 4+1;
col = n % 4;
hBtn = CreateWindow(_T("BUTTON"), t, WS_CHILD | WS_VISIBLE, col*W+10, row*H, w, H, hWnd, (HMENU)(1000 + id), NULL, hInst);
SendMessage(hBtn, WM_SETFONT, (WPARAM)hf, TRUE); //在创建完控件后面添加此句
n++;
}
void putForm(HWND hWnd) {
int w = W;
hf = CreateFont(24, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_BOLD, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
DEFAULT_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
TEXT("黑体"));
textBox = CreateWindow(_T("EDIT"), _T("0"), WS_VISIBLE | WS_CHILD | WS_BORDER | ES_READONLY | ES_RIGHT, 8, 4, 164, 33, hWnd, (HMENU)17, NULL, NULL);//创建文本框
SendMessage(textBox, WM_SETFONT, (WPARAM)hf, TRUE);
putControl(hWnd, _T("7"), w,7);
putControl(hWnd, _T("8"), w,8);
putControl(hWnd, _T("9"), w,9);
putControl(hWnd, _T("/"), w,14);
putControl(hWnd, _T("4"), w,4);
putControl(hWnd, _T("5"), w,5);
putControl(hWnd, _T("6"), w,6);
putControl(hWnd, _T("*"), w,13);
putControl(hWnd, _T("1"), w,1);
putControl(hWnd, _T("2"), w,2);
putControl(hWnd, _T("3"), w,3);
putControl(hWnd, _T("-"), w,12);
putControl(hWnd, _T("0"), w,0);
putControl(hWnd, _T("."), w,10);
putControl(hWnd, _T("+"), w+w,11);
n++;
putControl(hWnd, _T("="), w + w,15);
n++;
putControl(hWnd, _T("AC"), w + w,16);
}