Code:
//Includes the WinAPI
#include <windows.h>
// Variables
HWND hwndMain, hwndEdit, hwndButton; // Main window HWND and the edit window HWND
COLORREF crEditBg, crEditFg;
HBRUSH hbrEditBg;
int number = 5;
// Functions
//Callback function
LRESULT CALLBACK MainWndProc(HWND hwnd, // Handle of window which received this message
UINT msg, // The message
WPARAM wParam, // Extra Information
LPARAM lParam); // Extra Information
//The entry point of a windows application
int WINAPI
WinMain(HINSTANCE hInstance, /* Handle to the current instance*/
HINSTANCE hPrevInstance, /* Handle to the previuos instance*/
LPSTR lpCmdLine, /* Pointer to the command line arguements*/
int nCmdShow) /* Show state of the window wether the window should be maximised or minimised*/
{
MSG msg; // MSG structure to store messages
WNDCLASSEX wcx; // WINDOW class iunformation
// Fill in the WNDCLASS Struct
wcx.cbSize = sizeof(WNDCLASSEX); // Window size. Must always be sizeof(WNDCLASSEX)
wcx.style = 0; // Class Styles
wcx.lpfnWndProc = MainWndProc; // Pointer to the callback procedure******
wcx.cbClsExtra = 0; // Extra byte to allocate following the wndclassex structure
wcx.cbWndExtra = 0; // Extra byte to allocate following an instance of the structure
wcx.hInstance = hInstance; // Instance of the application
wcx.hIcon = LoadIcon (NULL, IDI_APPLICATION); //Class Icon
wcx.hCursor = LoadCursor (NULL, IDC_ARROW); //Class Cursor
wcx.hbrBackground = (HBRUSH) (COLOR_WINDOW+1); //Background brush/Colour
wcx.lpszMenuName = NULL; // Main Resource
wcx.lpszClassName = "Draw"; // Name of this class
wcx.hIconSm = NULL; // Small icon for the window
// Register class with MS-Windows
if(!RegisterClassEx(&wcx))
return 0;
//Create instances of the window now that it has been registered
hwndMain = CreateWindowEx(WS_EX_CLIENTEDGE, //Extended window style
"Draw", // Window class name
"Calculator", // Window Title
WS_OVERLAPPEDWINDOW, //Window Style
CW_USEDEFAULT, // Default X-position of window
CW_USEDEFAULT, // Default Y-position of window
170, // Width of the window
220, // Height of the window
NULL, // Handle of parent window
NULL, // Handle to menu
hInstance, // Handle to application instance
NULL); // Pointer to window creation data
// Initialise colors
crEditFg = RGB(0,0,0); // BLACK ON
crEditBg = RGB(255,255,255); // WHITE
// Create Edit window
hwndEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"0",
WS_BORDER|WS_VISIBLE|WS_CHILD|ES_NUMBER|ES_RIGHT,
0,0,160,30,
hwndMain,
NULL,
hInstance,
NULL);
/////////////Create Buttons Here//////////
hwndButton = CreateWindowEx(0,"BUTTON","1",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,0,110,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","2",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,40,110,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","3",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,80,110,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","4",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,0,70,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","5",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,40,70,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","6",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,80,70,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","7",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,0,30,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","8",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,40,30,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","9",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,80,30,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","0",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,0,150,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","+",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,120,30,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","-",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,120,70,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","*",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,120,110,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","/",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,120,150,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","=",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,40,150,40,40,hwndMain,NULL,hInstance,NULL);
hwndButton = CreateWindowEx(0,"BUTTON","C",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,80,150,40,40,hwndMain,NULL,hInstance,NULL);
ShowWindow(hwndMain,nCmdShow);
//^^^^Code above creates the window + edit box + buttons and shows them^^^^
//Process messages coming to this window
//this code is polling the system
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg); // Translates virtual key messages to character mesages
DispatchMessage(&msg);
}
//Return value to the system
return msg.wParam;
}
// Callback function for the Main Window class
LRESULT CALLBACK MainWndProc(HWND hwndMain, // Handle of window which received this message(you may have created more then 1 window)
UINT msg, // The message which has been sent
WPARAM wParam,
LPARAM lParam) // Extra information
{
switch(msg)
{
case WM_DESTROY: // This is the message sent WM_DESTROY
//User closed the window
PostQuitMessage(0); // The arguement to PostQuitMessage is the return value of the system
break;
//Code to show number in Edit Control
case WM_COMMAND:
if(LOWORD(wParam) == 1){
if(HIWORD(wParam) == BN_CLICKED){
SetDlgItemInt(HWND,hwndEdit,number,TRUE);
}
}
break;
default:
//Call the default window handler
return DefWindowProc(hwndMain,msg,wParam,lParam);
}
return 0;
}