#include <windows.h>
#include <stdio.h>
#include <commctrl.h>
#define IDC_BTN1 10000
#define IDC_BTN2 10001
#define IDC_EDIT1 10002
#define IDC_EDIT2 10003
#define IDC_EDIT3 10004
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "CodeBlocksWindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (0,szClassName,"Code::Blocks Template Windows App",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
450,125, HWND_DESKTOP, NULL, hThisInstance, NULL);
CreateWindowEx(0,WC_EDIT, "20.02", WS_CHILD|WS_VISIBLE, 20,20, 75,20, hwnd, (HMENU)IDC_EDIT1, hThisInstance, NULL);
CreateWindowEx(0,WC_BUTTON, "Plus", WS_CHILD|WS_VISIBLE, 120,20, 75,20, hwnd, (HMENU)IDC_BTN1, hThisInstance, NULL);
CreateWindowEx(0,WC_BUTTON, "Minus", WS_CHILD|WS_VISIBLE, 120,50, 75,20, hwnd, (HMENU)IDC_BTN2, hThisInstance, NULL);
CreateWindowEx(0,WC_EDIT, "10", WS_CHILD|WS_VISIBLE, 225,20, 75,20, hwnd, (HMENU)IDC_EDIT2, hThisInstance, NULL);
CreateWindowEx(0,WC_STATIC, "=", WS_CHILD|WS_VISIBLE, 320,20, 15,20, hwnd, (HMENU)NULL, hThisInstance, NULL);
CreateWindowEx(0,WC_EDIT, "0", WS_CHILD|WS_VISIBLE|ES_READONLY, 350,20, 55,20, hwnd, (HMENU)IDC_EDIT3, hThisInstance, NULL);
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
bool readInputs(HWND hwnd, float *num1, float *num2)
{
const int bufLen = 256;
char buffer[bufLen];
HWND editBox1, editBox2;
int numInputsConverted=0;
editBox1 = GetDlgItem(hwnd, IDC_EDIT1);
editBox2 = GetDlgItem(hwnd, IDC_EDIT2);
GetWindowText(editBox1, buffer, bufLen-1);
numInputsConverted = sscanf(buffer, "%f", num1);
if (numInputsConverted == 0)
return false;
GetWindowText(editBox2, buffer, bufLen-1);
numInputsConverted = sscanf(buffer, "%f", num2);
if (numInputsConverted == 0)
return false;
return true;
}
void handleBtn1(HWND hwnd)
{
float num1, num2, result;
bool validInputs;
const int bufLen = 256;
char buffer[bufLen];
HWND editBox3;
editBox3 = GetDlgItem(hwnd, IDC_EDIT3);
validInputs = readInputs(hwnd, &num1, &num2);
if (validInputs)
{
result = num1 + num2;
sprintf(buffer, "%0.2f", result);
SetWindowText(editBox3, buffer);
}
else
MessageBox(hwnd, "Please enter two valid numbers", "Error", MB_ICONERROR|MB_OK);
}
void handleBtn2(HWND hwnd)
{
float num1, num2, result;
bool validInputs;
const int bufLen = 256;
char buffer[bufLen];
HWND editBox3;
editBox3 = GetDlgItem(hwnd, IDC_EDIT3);
validInputs = readInputs(hwnd, &num1, &num2);
if (validInputs)
{
result = num1 - num2;
sprintf(buffer, "%0.2f", result);
SetWindowText(editBox3, buffer);
}
else
MessageBox(hwnd, "Please enter two valid numbers", "Error", MB_ICONERROR|MB_OK);
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_COMMAND:
switch LOWORD(wParam)
{
case IDC_BTN1:
handleBtn1(hwnd);
break;
case IDC_BTN2:
handleBtn2(hwnd);
break;
}
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}</commctrl.h></stdio.h></windows.h>