📄 wincalc.cpp
字号:
// WinCalc.cpp : Defines the entry point for the application.
//
/////////////////////////////includes/////////////////////////////////
#include "stdafx.h"
#include "Header.h"
#include "jishuqi.h"
#include <fstream>
using namespace std;
////////////////////////////definitions///////////////////////////////
//button description structure
typedef struct stBTNDESC
{
HWND hWnd;
int iX;
int iY;
int iWidth;
int iHeight;
char szText[4];
}BTNDESC;
//global variables
HWND hWnd = NULL; //main window handle
HINSTANCE hInst = NULL; //main instance
BTNDESC bdBtns[MAX_BUTTONS]; //button description struct
HWND hEdit = NULL; //window handle of the edit
HMENU hMenu = NULL; //handle of the main manu
bool bReset = false; //if the expression has been reset
//('=' button pushed)
static TCHAR szAppName[] = TEXT("WinCalc"); //application name
/*/////////////////////////::DlgProc//////////////////////////////////
DESCRIPTION: window proc for the dialog "About"(IDD_ABOUT)
BEHAVIOR: - close the dialog itself when user pushes "OK" or
the close button
*/////////////////////////////////////////////////////////////////////
int CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
return 1;
case WM_COMMAND:
if(LOWORD(wParam) == IDOK)
EndDialog(hDlg, 0);
break;
case WM_CLOSE:
EndDialog(hDlg, 0);
break;
default:
break;
}
return 0;
}
/*/////////////////////////::WndProc//////////////////////////////////
DESCRIPTION: window proc for the main win32
BEHAVIOR: - wm_creation:
- create the edit box
- create the buttons
- move the main window to (320, 200)
- wm_command:
- show the help message when user clicked on
"help" in the menu items
- show the about dialog when user clicked on
"about WinCalc" in the menu items
- do the right behavior when user clicked on
a button
- end the application when the after window
was destroyed
*/////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch (message)
{
case WM_CREATE:
{
//create the edit
hEdit = ::CreateWindow("edit", "0",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_RIGHT,
0, 0,
192, 20,
hwnd, NULL, hInst, NULL);
//create the buttons
for(int i=0; i < MAX_BUTTONS; i++)
bdBtns[i].hWnd = ::CreateWindow("button", bdBtns[i].szText,
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
bdBtns[i].iX, bdBtns[i].iY,
bdBtns[i].iWidth, bdBtns[i].iHeight,
hwnd, NULL, hInst, NULL);
::MoveWindow(hwnd, 320, 200, WNDWIDTH, WNDHEIGHT, true);
bReset = true;
}return 0;
case WM_COMMAND:
{
//menu item been clicked
if(LOWORD(wParam) == ID_COPY)
{
::SendMessage(hEdit, WM_COPY, 0, 0);
return 0;
}
else if(LOWORD(wParam) == ID_PASTE)
{
::SendMessage(hEdit, WM_PASTE, 0, 0);
return 0;
}
else if(LOWORD(wParam) == ID_ABOUT_README)
{
::DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUT), hwnd, DlgProc);
return 0;
}
else if(LOWORD(wParam) == ID_ABOUT_HELP)
{
::DialogBox(hInst, MAKEINTRESOURCE(IDD_HELP), hwnd, DlgProc);
return 0;
}
//edit been clicked
if((HWND)lParam == hEdit)
{
if(HIWORD(wParam) == EN_SETFOCUS) //get the focus
{
::SendMessage(hEdit, EM_SETSEL, (WPARAM)0, (LPARAM)-1);
return 0;
}
}
//buttons been clicked
if((HWND)lParam != hEdit && wParam == BN_CLICKED)
{
char pstr[64];
char pbuf[4];
double dValue = 0;
int istrnum = 0;
int ir = 0; //returned value
char pMsg[64]; //error message
for(int i=0; i < MAX_BUTTONS; i++)
{
if((HWND)lParam == bdBtns[5].hWnd) //"="
{
if(bReset)
return 0;
else
{
::GetWindowText(hEdit, pstr,
64);
if(ir = mainfunc(pstr, &dValue), ir)
{
//output the error messages
switch(ir)
{
case 1:
strcpy(pMsg, "括号不匹配");
break;
case 2:
strcpy(pMsg, "表达式中含有空格");
break;
case 3:
strcpy(pMsg, "不合法的表达式");
break;
case 4:
strcpy(pMsg, "对负数求平方根");
break;
case 5:
strcpy(pMsg, "参数为负数, 参数不足或表达式不符合说明(x1>=x2)");
break;
case 6:
strcpy(pMsg, "对负数求阶乘");
break;
case 7:
strcpy(pMsg, "对非整数求排列, 组合或阶乘");
break;
case 8:
strcpy(pMsg, "二元运算符参数不足");
break;
default:
strcpy(pMsg, "其它未知的错误");
break;
}
::MessageBox(hwnd, pMsg, "错误", MB_OK);
}
//format data to string
sprintf(pstr, "%f", dValue);
//clean the extra zeroes
istrnum = strlen(pstr);
while(pstr[istrnum - 1] == '0')
istrnum--;
if(pstr[istrnum - 1] == '.')
pstr[istrnum - 1] = '\0';
else
pstr[istrnum] = '\0';
//display the value
::SetWindowText(hEdit, pstr);
//get ready for the next calculation
bReset = true;
return 0;
}
}
if((HWND)lParam == bdBtns[23].hWnd) //"<-"
{
if(bReset)
{
strcpy(pstr, "0");
::SetWindowText(hEdit, pstr);
return 0;
}
else
{
::GetWindowText(hEdit, pstr,
64);
istrnum = strlen(pstr);
if(istrnum == 1)
{
pstr[0] = '0';
bReset = true;
}
else
{
if(istrnum > 1 &&
pstr[istrnum - 2] == '.')
pstr[istrnum - 2] =
'\0';
else
pstr[istrnum - 1] =
'\0';
}
::SetWindowText(hEdit, pstr);
return 0;
}
}
if((HWND)lParam == bdBtns[29].hWnd) //"CE"
{
bReset = true;
::SetWindowText(hEdit, "0");
return 0;
}
if((HWND)lParam == bdBtns[i].hWnd) //other
{ //buttons
::GetWindowText(hEdit, pstr, 64);
::GetWindowText(bdBtns[i].hWnd, pbuf, 4);
if(!bReset)
strcat(pstr, pbuf);
else
strcpy(pstr, pbuf);
::SetWindowText(hEdit,pstr);
bReset = false;
return 0;
}
}//for
}//if
}//case
break;
case WM_KEYDOWN:
{
}break;
case WM_PAINT:
{
hdc = BeginPaint (hwnd, &ps);
EndPaint (hwnd, &ps);
}return 0;
case WM_DESTROY:
{
PostQuitMessage (0);
}return 0;
default:
break;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
/*//////////////////////////::InitApp/////////////////////////////////
DESCRIPTION: global function that initialize the win32 app
BEHAVIOR: - set the properties for the buttons
- register the
*/////////////////////////////////////////////////////////////////////
bool InitApp()
{
for(int i=0; i < MAX_BUTTONS; i++)
{
bdBtns[i].hWnd = NULL;
bdBtns[i].iWidth = 32;
bdBtns[i].iHeight = 32;
strcpy(bdBtns[i].szText, " ");
}
for(int y = 0; y < 5; y++)
{
for(int x = 0; x < 6; x++)
{
bdBtns[x + y * 6].iX = x * 32;
bdBtns[x + y * 6].iY = 164 - y * 32 - 16;
}
}
strcpy(bdBtns[0].szText, "{");
strcpy(bdBtns[1].szText, "}");
strcpy(bdBtns[2].szText, "0");
strcpy(bdBtns[3].szText, ",");
strcpy(bdBtns[4].szText, ".");
strcpy(bdBtns[5].szText, "=");
strcpy(bdBtns[6].szText, "[");
strcpy(bdBtns[7].szText, "]");
strcpy(bdBtns[11].szText, "+");
strcpy(bdBtns[12].szText, "(");
strcpy(bdBtns[13].szText, ")");
strcpy(bdBtns[17].szText, "-");
strcpy(bdBtns[18].szText, "A");
strcpy(bdBtns[19].szText, "C");
strcpy(bdBtns[23].szText, "<-");
strcpy(bdBtns[24].szText, "S");
strcpy(bdBtns[25].szText, "!");
strcpy(bdBtns[26].szText, "^");
strcpy(bdBtns[27].szText, "/");
strcpy(bdBtns[28].szText, "*");
strcpy(bdBtns[29].szText, "CE");
char buffer[4];
for(int y2 = 0; y2 < 3; y2++)
{
for(int x2 = 0; x2 < 3; x2++)
{
itoa(x2 + 1 + y2 * 3, buffer, 10);
strcpy(bdBtns[x2 + 2 + (y2 + 1) * 6].szText, buffer);
}
}
WNDCLASS wndclass;
wndclass.style = 0;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInst;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (GRAY_BRUSH);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox (NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hMenu = ::LoadMenu(hInst, MAKEINTRESOURCE(IDR_MENU));
hWnd = CreateWindow(szAppName, // window class name
TEXT("WinCalc"), // window caption
WS_OVERLAPPED | WS_CAPTION |
WS_SYSMENU, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
WNDWIDTH, // initial x size
WNDHEIGHT, // initial y size
NULL, // parent window handle
hMenu, // window menu handle
hInst, // program instance handle
NULL); // creation parameters
return true;
}
/*//////////////////////////::WinMain/////////////////////////////////
DESCRIPTION: main entry point of the win32 application
BEHAVIOR: - put down some debug infomation
- call the initializing function
- enter the message loop
*/////////////////////////////////////////////////////////////////////
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MSG msg;
fstream f("log.txt", ios::out | ios::trunc);
f << "WinCalc " << VERSION << endl << "Author: xsown && Xie" << endl << endl;
f.close();
hInst = hInstance;
InitApp();
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/*////////////////////////////::log///////////////////////////////////
DESCRIPTION: file debugger
BEHAVIOR: - add a string into "log.txt"
*/////////////////////////////////////////////////////////////////////
void log(char* p)
{
fstream f("log.txt", ios::out | ios::app);
f << p << endl;
f.close();
}
/*////////////////////////////::log///////////////////////////////////
DESCRIPTION: file debugger
BEHAVIOR: - add an integer into "log.txt"
*/////////////////////////////////////////////////////////////////////
void log(int i)
{
fstream f("log.txt", ios::out | ios::app);
f << i << endl;
f.close();
}
/*////////////////////////////::log///////////////////////////////////
DESCRIPTION: file debugger
BEHAVIOR: - add a character into "log.txt"
*/////////////////////////////////////////////////////////////////////
void log(char p)
{
fstream f("log.txt", ios::out | ios::app);
f << p << '(' << (int)p << ')' << endl;
f.close();
}
/*////////////////////////////::log///////////////////////////////////
DESCRIPTION: file debugger
BEHAVIOR: - add a unsigned character into "log.txt"
*/////////////////////////////////////////////////////////////////////
void log(UCHAR p)
{
fstream f("log.txt", ios::out | ios::app);
f << p << '(' << (int)p << ')' << endl;
f.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -