1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <Windows.h>
#define ID_CALCULATE 101
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND hwnd,editnum1,editnum2,edittotal,buttoncalclulate;
WNDCLASSEX wc;
MSG msg;
HINSTANCE hinst;
int APIENTRY WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR line, int show)
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinst;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDI_APPLICATION);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW );
wc.lpszClassName = TEXT("simplecalculator");
wc.lpszMenuName = NULL;
wc.lpfnWndProc = WndProc;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
hwnd = CreateWindowEx(NULL, TEXT("simplecalculator"), TEXT("Simple Calculator"),WS_SYSMENU|WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 790, 150, NULL, NULL, hinst, NULL);
ShowWindow(hwnd, show);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_CREATE:
editnum1 = CreateWindow(TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE, 20, 20, 200, 30,hwnd, NULL, hinst, NULL);
editnum2 = CreateWindow(TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE, 230, 20, 200, 30, hwnd, NULL, hinst, NULL);
edittotal = CreateWindow(TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE, 550, 20, 200, 30, hwnd, NULL, hinst, NULL);
buttoncalclulate = CreateWindow(TEXT("button"), "Calculate", WS_CHILD | WS_VISIBLE|BS_FLAT, 440, 20, 100, 30, hwnd, (HMENU)ID_CALCULATE, hinst, NULL);
break;
case WM_COMMAND:
if (LOWORD(wparam) == ID_CALCULATE)
{
BYTE maxChars = 10;
TCHAR * buffer = new TCHAR[maxChars];
int writtenChars = GetWindowText(editnum1, buffer, maxChars);
float num1 = _tcstof(buffer, NULL);
BYTE maxChars2 = 10;
TCHAR * buffer2 = new TCHAR[maxChars2];
int writtenChars2 = GetWindowText(editnum1, buffer2, maxChars2);
float num2 = _tcstof(buffer2, NULL);
float total = num1 + num2;
char display[1];
_itoa(total, display, 100000);
SetWindowTextA(edittotal, display);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
break;
}
return 0;
}
|