(Best in 1280 x 960 resolution mode or higher)
On Windows Programming (Page 2)
On to windows programming (page 3)
/* Calculator Program Revisited June 7th, 2006 */
#include <windows.h> #include <cstring> using namespace std; //already include <stdlib.h> when using namespace std; //Data conversion routine is atof() function convert strings to //floating-point numbers. The syntax for the atof() function is //variable = atof(string); void setNumbers(char szCurNum[10]); void runCalculation(); char cOperation = '0'; char szFirstNum[10]; double dFirstNum = 0; char szSecondNum[10]; double dSecondNum = 0;
LRESULT CALLBACK WINAPI MainWndProc (HWND, UINT, WPARAM, LPARAM); HWND hWnd; HWND hwndEdit; HWND hwndButtonPlus; HWND hwndButtonMinus; HWND hwndButtonMultiply; HWND hwndButtonDivide; HWND hwndButton9; HWND hwndButton8; HWND hwndButton7; HWND hwndButton6; HWND hwndButton5; HWND hwndButton4; HWND hwndButton3; HWND hwndButton2; HWND hwndButton1; HWND hwndButton0; HWND hwndButtonPoint; HWND hwndButtonEquals; HWND hwndButtonClear;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc; wc.lpszClassName = "CalculatorClass"; wc.lpfnWndProc = MainWndProc; wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wc.lpszMenuName = ""; wc.cbClsExtra = 0; wc.cbWndExtra = 0; RegisterClass(&wc);
hWnd = CreateWindow("CalculatorClass", "Calculator", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 185, 265, NULL, NULL, hInstance, NULL); hwndEdit = CreateWindow("EDIT", NULL, WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, 10, 10, 155, 20, hWnd, NULL, hInstance, NULL); hwndButtonPlus = CreateWindow( "BUTTON", "+", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 10, 40, 35, 35, hWnd, NULL, hInstance, NULL); hwndButtonMinus = CreateWindow( "BUTTON", "-", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 50, 40, 35, 35, hWnd, NULL, hInstance, NULL); hwndButtonMultiply = CreateWindow( "BUTTON", "*", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 90, 40, 35, 35, hWnd, NULL, hInstance, NULL); hwndButtonDivide = CreateWindow( "BUTTON", "/", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 130, 40, 35, 35, hWnd, NULL, hInstance, NULL); hwndButton6 = CreateWindow( "BUTTON", "6", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 10, 80, 35, 35, hWnd, NULL, hInstance, NULL); hwndButton7 = CreateWindow( "BUTTON", "7", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 50, 80, 35, 35, hWnd, NULL, hInstance, NULL); hwndButton8 = CreateWindow( "BUTTON", "8", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 90, 80, 35, 35, hWnd, NULL, hInstance, NULL); hwndButton9 = CreateWindow( "BUTTON", "9", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 130, 80, 35, 35, hWnd, NULL, hInstance, NULL); hwndButton2 = CreateWindow( "BUTTON", "2", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 10, 120, 35, 35, hWnd, NULL, hInstance, NULL); hwndButton3 = CreateWindow( "BUTTON", "3", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 50, 120, 35, 35, hWnd, NULL, hInstance, NULL); hwndButton4 = CreateWindow( "BUTTON", "4", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 90, 120, 35, 35, hWnd, NULL, hInstance, NULL); hwndButton5 = CreateWindow( "BUTTON", "5", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 130, 120, 35, 35, hWnd, NULL, hInstance, NULL); hwndButton0 = CreateWindow( "BUTTON", "0", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 10, 160, 35, 35, hWnd, NULL, hInstance, NULL); hwndButton1 = CreateWindow( "BUTTON", "1", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 50, 160, 35, 35, hWnd, NULL, hInstance, NULL); hwndButtonPoint = CreateWindow( "BUTTON", ".", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 90, 160, 35, 35, hWnd, NULL, hInstance, NULL); hwndButtonEquals = CreateWindow( "BUTTON", "=", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 130, 160, 35, 35, hWnd, NULL, hInstance, NULL); hwndButtonClear = CreateWindow( "BUTTON", "Clear", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 10, 200, 155, 25, hWnd, NULL, hInstance, NULL);
ShowWindow (hWnd, nCmdShow); MSG msg; BOOL bRet; while ((bRet = GetMessage ( &msg, NULL, 0, 0)) !=0) { if (bRet == -1) { //handle this error and probably exit break; } else { TranslateMessage (&msg); DispatchMessage (&msg); } } return (int) msg.wParam; }
LRESULT CALLBACK WINAPI MainWndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { HWND hwndCtl = (HWND) lParam; switch (msg) { case WM_COMMAND: switch (wParam) { case BN_CLICKED: if (hwndCtl == hwndButton1) setNumbers("1"); else if (hwndCtl == hwndButton2) setNumbers("2"); else if (hwndCtl == hwndButton3) setNumbers("3"); else if (hwndCtl == hwndButton4) setNumbers("4"); else if (hwndCtl == hwndButton5) setNumbers("5"); else if (hwndCtl == hwndButton6) setNumbers("6"); else if (hwndCtl == hwndButton7) setNumbers("7"); else if (hwndCtl == hwndButton8) setNumbers("8"); else if (hwndCtl == hwndButton9) setNumbers("9"); else if (hwndCtl == hwndButton0) setNumbers("0"); else if (hwndCtl == hwndButtonPoint) setNumbers("."); else if (hwndCtl == hwndButtonPlus) { cOperation = '+'; dFirstNum = atof(szFirstNum); } else if (hwndCtl == hwndButtonMinus) { cOperation = '-'; dFirstNum = atof(szFirstNum); } else if (hwndCtl == hwndButtonMultiply) { cOperation = '*'; dFirstNum = atof(szFirstNum); } else if (hwndCtl == hwndButtonDivide) { cOperation = '/'; dFirstNum = atof(szFirstNum); } else if (hwndCtl == hwndButtonEquals) { dSecondNum = atof(szSecondNum); runCalculation(); } else if (hwndCtl == hwndButtonClear) { cOperation = '0'; strcpy(szFirstNum, ""); dFirstNum = 0; strcpy(szSecondNum, ""); dSecondNum = 0; SetWindowText(hwndEdit, ""); } } break; case WM_DESTROY: PostQuitMessage(0); return 0; default: return DefWindowProc(hWnd, msg, wParam, lParam); } return 0; }
void setNumbers(char szCurNum[10]) { if (cOperation == '0') { strcat(szFirstNum, szCurNum); SetWindowText(hwndEdit, szFirstNum); } else { strcat(szSecondNum, szCurNum); SetWindowText(hwndEdit, szSecondNum); } }
void runCalculation() { double dResult = 0; char szResult[25]; if (cOperation == '+') { dResult = dFirstNum + dSecondNum; } else if (cOperation == '-') { dResult = dFirstNum - dSecondNum; } else if (cOperation == '*') { dResult = dFirstNum * dSecondNum; } else if (cOperation == '/') { dResult = dFirstNum / dSecondNum; } _gcvt(dResult, 10, szResult); SetWindowText(hwndEdit, szResult); cOperation = '0'; strcpy(szFirstNum, szResult); dFirstNum = 0; strcpy(szSecondNum, ""); dSecondNum = 0; }
|
August 29, 2006