/* The original version of this program can be found at http://damb.dk */
#include <windows.h>
#include <sstream>
enum WindowsId
{
FirstEditId = 256,
SecondEditId,
ResultEditId,
ButtonId
};
HINSTANCE InstanceHandle;
int NumPush = 0;
template <typename T>
bool GetDlgItemNumber(HWND aHwndDlg, int aDlgItemId, T &aVal)
{
char Buffer[256];
if(!GetDlgItemText(aHwndDlg, aDlgItemId, Buffer, sizeof(Buffer)))
return false;
std::stringstream ss(Buffer);
return ss >> aVal;
}
template <typename T>
bool SetDlgItemNumber(HWND aHwndDlg, int aDlgItemId, const T &aValue)
{
std::stringstream ss;
ss << aValue;
return SetDlgItemText(aHwndDlg, aDlgItemId, ss.str().c_str());
}
LRESULT CALLBACK DialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"", // Initial Text
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP, // Style
5, 5, 120, 20, // position
hwndDlg, // Owner
(HMENU)(FirstEditId), // ID
InstanceHandle, // The application
0);
CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"", // Initial Text
WS_CHILD | WS_VISIBLE | WS_TABSTOP, // Style
5, 35, 120, 20, // position
hwndDlg, // Owner
(HMENU)(SecondEditId), // ID
InstanceHandle, // The application
0);
CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"0", // Initial Text
WS_CHILD | WS_VISIBLE | ES_NUMBER | ES_READONLY, // Style
5, 65, 120, 20, // position
hwndDlg, // Owner
(HMENU)(ResultEditId), // ID
InstanceHandle, // The application
0);
CreateWindow("BUTTON",
"Add", // Button Text
WS_CHILD | WS_VISIBLE | WS_TABSTOP, // Style
5, 90, 120, 30, // position
hwndDlg, // Owner
(HMENU)(ButtonId), // ID
InstanceHandle, // The application
0);
break;
case WM_COMMAND:
if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == ButtonId)
{
double First;
double Second;
if(!GetDlgItemNumber(hwndDlg, FirstEditId, First))
{
MessageBox(hwndDlg, "Failed to convert first string", "Calculator", MB_OK);
}
else
{
if(!GetDlgItemNumber(hwndDlg, SecondEditId, Second))
{
MessageBox(hwndDlg, "Failed to convert second string", "Calculator", MB_OK);
}
else
{
double Result = First + Second;
if(!SetDlgItemNumber(hwndDlg, ResultEditId, Result))
{
MessageBox(hwndDlg, "Failed to set result", "Calculator", MB_OK);
}
}
}
}
break;
}
return DefWindowProc(hwndDlg, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
InstanceHandle = hInstance;
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = DialogProc;
wc.hInstance = InstanceHandle;
wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
wc.lpszClassName = "WhateverClass";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
if(!RegisterClass(&wc))
return FALSE;
HWND WindowHandle = CreateWindow("WhateverClass",
"Calc", // Caption text
WS_MINIMIZEBOX | WS_VISIBLE | WS_CLIPSIBLINGS |
WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_CAPTION | WS_BORDER | WS_SYSMENU,
100, 100, 140, 160, // Position
NULL,
NULL,
InstanceHandle,
0);
MSG Msg;
while(GetMessage(&Msg, WindowHandle, 0, 0xFFFF) > 0)
{
if(!IsDialogMessage(WindowHandle, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return 0;
}