Code:
#include <iostream>
#include <Windows.h>
#include <tchar.h>
int setupWindow(HINSTANCE, int);
int setupControls(HINSTANCE);
void calculateResult();
HWND hwnd;
HWND hwndText1;
HWND hwndText2;
HWND hwndButton;
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg,WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
if(lParam == (LPARAM)hwndButton)
{
calculateResult();
}
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
if(!setupWindow(hInstance, nCmdShow))
{
return -1;
}
if(!setupControls(hInstance))
{
return -1;
}
// Message loop
MSG msg;
while(GetMessage(&msg, hwnd, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
int setupWindow(HINSTANCE hInstance, int nCmdShow)
{
WNDCLASSEX wndclass;
ZeroMemory(&wndclass, sizeof(wndclass));
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.hInstance = hInstance;
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszClassName = L"vbfsampleclass";
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.lpfnWndProc = WndProc;
if(!RegisterClassEx(&wndclass))
{
DWORD err = GetLastError();
std::cerr << "RegisterClassEx failed with error code " << GetLastError << std::endl;
return 0;
}
hwnd = CreateWindowEx(
0,
L"vbfsampleclass",
L"VBForums Win32 sample",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if(hwnd == NULL)
{
std::cerr << "CreateWindowEx failed with error code " << GetLastError() << std::endl;
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return 1;
}
int setupControls(HINSTANCE hInstance)
{
// Textbox 1
hwndText1 = CreateWindowEx(
0,
L"EDIT",
L"3",
ES_NUMBER | WS_CHILD | WS_VISIBLE | WS_BORDER,
16,
24,
100,
24,
hwnd,
(HMENU)101,
hInstance,
NULL);
if(hwndText1 == NULL)
{
std::cerr << "CreateWindowEx failed with error code " << GetLastError() << std::endl;
return 0;
}
// Textbox 2
hwndText2 = CreateWindowEx(
0,
L"EDIT",
L"5",
ES_NUMBER | WS_CHILD | WS_VISIBLE | WS_BORDER,
16,
60,
100,
24,
hwnd,
(HMENU)101,
hInstance,
NULL);
if(hwndText2 == NULL)
{
std::cerr << "CreateWindowEx failed with error code " << GetLastError() << std::endl;
return 0;
}
// Button 1
hwndButton = CreateWindowEx(
0,
L"BUTTON",
L"Calculate!",
BS_TEXT | BS_VCENTER | WS_CHILD | WS_VISIBLE | WS_BORDER,
16,
96,
100,
24,
hwnd,
0,
hInstance,
NULL);
if(hwndButton == NULL)
{
std::cerr << "CreateWindowEx failed with error code " << GetLastError() << std::endl;
return 0;
}
ShowWindow(hwndButton, SW_SHOW);
return 1;
}
void calculateResult()
{
TCHAR strValue1[64];
TCHAR strValue2[64];
TCHAR strResult[64];
int intValue1;
int intValue2;
GetWindowText(hwndText1, strValue1, 64);
GetWindowText(hwndText2, strValue2, 64);
intValue1 = _tstoi(strValue1);
intValue2 = _tstoi(strValue2);
_itot(intValue1 + intValue2, strResult, 10);
SetWindowText(hwndButton, strResult);
}