Code:
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "calculator";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"calculator", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
207, /* The programs width */
282, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
class Calculator
{
public:
//Calculator(hwnd);
void command(WPARAM, LPARAM);
void create(HWND);
int destroy();
//private:
HWND hNumbers[10], hOperators[4],
hDecimal, hClear, hStatic;
double subtotal;
};
enum keypad
{
ID_Button_0,
ID_Button_1, ID_Button_2, ID_Button_3,
ID_Button_4, ID_Button_5, ID_Button_6,
ID_Button_7, ID_Button_8, ID_Button_9,
ID_Button_ADD, ID_Button_SUB,
ID_Button_MULT, ID_Button_DIV,
ID_Button_CLEAR, ID_Static
};
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
Calculator MyCalc;
switch (message) /* handle the messages */
{
case WM_CREATE: MyCalc.create(hwnd); break;
case WM_COMMAND: MyCalc.command(wParam, lParam); break;
case WM_DESTROY: MyCalc.destroy(); break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
//Function Definitions
void Calculator::create(HWND hwnd)
{
int index = 1;
TCHAR *numbers[] = {"1","2","3","4","5","6","7","8","9"};
for(int y=150; y>49; y-=50)
for(int x=0; x<101; x+=50)
{
hNumbers[index] = CreateWindow("BUTTON", numbers[index-1],
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
x, y, 50, 50, hwnd, (HMENU)index,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
index++;
}
index = 0;
TCHAR *operators[] = {"+","-","*","/"};
int x = 150, y = 0;
int ID = ID_Button_ADD;
for(int i=0; i<4; i++)
{
hOperators[i] = CreateWindow("BUTTON", operators[i],
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
x, y+=50, 50, 50, hwnd, (HMENU)ID++,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
hNumbers[0] = CreateWindow("BUTTON", "0", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
0, 200, 50, 50, hwnd, (HMENU)ID_Button_0,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
hDecimal = CreateWindow("BUTTON", ".", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
50, 200, 50, 50, hwnd, (HMENU)ID_Button_0,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
hClear = CreateWindow("BUTTON", "Clear", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
100, 200, 50, 50, hwnd, (HMENU)ID_Button_0,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
hStatic = CreateWindow("STATIC", "edit" , WS_CHILD|WS_VISIBLE|WS_BORDER,
5, 10, 188, 20, hwnd, (HMENU)ID_Static,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
}
void Calculator::command(WPARAM wParam, LPARAM lParam)
{
WORD msg = LOWORD(wParam);
TCHAR *numbers[] = {"1","2","3","4","5","6","7","8","9"};
//if(msg /* >= ID_Button_0 && msg <= ID_Button_9 */)
//Experimenting with using either a static window or an edit box
//SendMessage(hEditBox, WM_SETTEXT, 0, (LPARAM)numbers[msg]);
SetWindowText(hStatic, numbers[msg]);
}
int Calculator::destroy()
{
PostQuitMessage(0);
return 0;
}