#include <windows.h>
#include <stdlib.h>
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow) {
static TCHAR szAppName[] = TEXT("dirDemo");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
hInst = hInstance;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = COLOR_BTNFACE + 1;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("Register failure..."),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
500,
300,
300,
260,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam) {
static HWND hwndNumber[16], hwndStatic,
hwndClear, hwndEqual;
static cxChar, cyChar;
int i, col, row;
static double leftNumber, rightNumber;
static char left[20], right[20], buffer[20];
static char operators = '\0';
static BOOL bOperators, bEqual;
HBRUSH hBrush;
HDC hdc;
TCHAR* bufferIcon[16] = {
"7",
"8",
"9",
"/",
"4",
"5",
"6",
"*",
"1",
"2",
"3",
"-",
"0",
"+/-",
".",
"+"
};
switch(message) {
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());
col = row = 0;
left[0] = ' ';
right[0] = ' ';
for(i = 0; i < 16; i++) {
row = i / 4;
col = i % 4;
hwndNumber[i] = CreateWindow(TEXT("button"),
bufferIcon[i],
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
13 + col * (60 + 8), 80 + row * (30 + 8),
60, 30,
hwnd,
(HMENU)i,
hInst,
NULL);
}
hwndStatic = CreateWindow(TEXT("static"),
TEXT("0"),
WS_CHILD | WS_VISIBLE | SS_RIGHT,
13, 8,
270, 20,
hwnd,
(HMENU)16,
hInst,
NULL);
hwndClear = CreateWindow(TEXT("button"),
TEXT("C"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
13, 46,
128, 30,
hwnd,
(HMENU)17,
hInst,
NULL);
hwndEqual = CreateWindow(TEXT("button"),
TEXT("="),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
149, 46,
128, 30,
hwnd,
(HMENU)18,
hInst,
NULL);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam)) {
case 0:
if(!bOperators) {
strcat(left, "7");
SetWindowText(hwndStatic, left);
} else {
strcat(right, "7");
SetWindowText(hwndStatic, right);
}
break;
case 1:
if(!bOperators) {
strcat(left, "8");
SetWindowText(hwndStatic, left);
} else {
strcat(right, "8");
SetWindowText(hwndStatic, right);
}
break;
case 2:
if(!bOperators) {
strcat(left, "9");
SetWindowText(hwndStatic, left);
} else {
strcat(right, "9");
SetWindowText(hwndStatic, right);
}
break;
case 3:
bOperators = TRUE;
operators = '/';
break;
case 4:
if(!bOperators) {
lstrcat(left, "4");
SetWindowText(hwndStatic, left);
} else {
lstrcat(right, "4");
SetWindowText(hwndStatic, right);
}
break;
case 5:
if(!bOperators) {
lstrcat(left, "5");
SetWindowText(hwndStatic, left);
} else {
lstrcat(right, "5");
SetWindowText(hwndStatic, right);
}
break;
case 6:
if(!bOperators) {
lstrcat(left, "6");
SetWindowText(hwndStatic, left);
} else {
lstrcat(right, "6");
SetWindowText(hwndStatic, right);
}
break;
case 7:
bOperators = TRUE;
operators = '*';
break;
case 8:
if(!bOperators) {
strcat(left, "1");
SetWindowText(hwndStatic, left);
} else {
strcat(right, "1");
SetWindowText(hwndStatic, right);
}
break;
case 9:
if(!bOperators) {
strcat(left, "2");
SetWindowText(hwndStatic, left);
} else {
strcat(right, "2");
SetWindowText(hwndStatic, right);
}
break;
case 10:
if(!bOperators) {
strcat(left, "3");
SetWindowText(hwndStatic, left);
} else {
strcat(right, "3");
SetWindowText(hwndStatic, right);
}
break;
case 11:
bOperators = TRUE;
operators = '-';
break;
case 12:
if(!bOperators) {
strcat(left, "0");
SetWindowText(hwndStatic, left);
} else {
strcat(right, "0");
SetWindowText(hwndStatic, right);
}
break;
case 13:
if(!bOperators) {
if(left[0] == '-') {
left[0] = ' ';
SetWindowText(hwndStatic, left);
} else {
left[0] = '-';
SetWindowText(hwndStatic, left);
}
}
else {
if(right[0] == '-') {
right[0] = ' ';
SetWindowText(hwndStatic, right);
} else {
right[0] = '-';
SetWindowText(hwndStatic, right);
}
}
break;
case 14:
if(!bOperators) {
strcat(left, ".");
SetWindowText(hwndStatic, left);
} else {
lstrcat(right, ".");
SetWindowText(hwndStatic, right);
}
break;
case 15:
bOperators = TRUE;
operators = '+';
break;
/*
* case 16:
* static control, we needn't handle it
*/
case 17:
left[0] = ' ';
right[0] = ' ';
memset(left + 1, '\0', sizeof(left));
memset(right + 1, '\0', sizeof(right));
bOperators = FALSE;
SetWindowText(hwndStatic, "0");
operators = '\0';
bEqual = FALSE;
break;
case 18:
leftNumber = atof(left);
rightNumber = atof(right);
bEqual = TRUE;
right[0] = ' ';
memset(right + 1, '\0', sizeof(right));
bOperators = FALSE;
switch(operators) {
case '+':
leftNumber = leftNumber + rightNumber;
sprintf(buffer, "%f", leftNumber);
SetWindowText(hwndStatic, buffer);
if(leftNumber >= 0)
strcpy(left + 1, buffer);
else
strcpy(left, buffer);
break;
case '-':
leftNumber = leftNumber - rightNumber;
sprintf(buffer, "%f", leftNumber);
SetWindowText(hwndStatic, buffer);
if(leftNumber >= 0)
strcpy(left + 1, buffer);
else
strcpy(left, buffer);
break;
case '*':
leftNumber = leftNumber * rightNumber;
sprintf(buffer, "%f", leftNumber);
SetWindowText(hwndStatic, buffer);
if(leftNumber >= 0)
strcpy(left + 1, buffer);
else
strcpy(left, buffer);
break;
case '/':
if(rightNumber == 0) {
MessageBox(NULL, TEXT("郁闷,你竟然让我除以0, 我不活了!"),
TEXT("我在抱怨"), MB_ICONINFORMATION);
SendMessage(hwnd, WM_COMMAND,(WPARAM)17, 0);
break;
}
leftNumber = leftNumber / rightNumber;
sprintf(buffer, "%f", leftNumber);
SetWindowText(hwndStatic, buffer);
if(leftNumber >= 0)
strcpy(left + 1, buffer);
else
strcpy(left, buffer);
break;
default:
break;
}
break;
}
SetFocus(hwnd);
return 0;
case WM_CTLCOLORSTATIC:
SetTextColor(hdc, GetSysColor(COLOR_BTNTEXT));
SetBkColor(hdc, RGB(255, 255, 255));
hBrush = CreateSolidBrush(RGB(255, 255, 255));
return (LRESULT)hBrush;
case WM_CHAR:
for(i = 0; i < LOWORD(lParam); i++) {
switch(wParam) {
case '7':
SendMessage(hwnd, WM_COMMAND, 0, 0);
break;
case '8':
SendMessage(hwnd, WM_COMMAND, 1, 0);
break;
case '9':
SendMessage(hwnd, WM_COMMAND, 2, 0);
break;
case '/':
SendMessage(hwnd, WM_COMMAND, 3, 0);
break;
case '4':
SendMessage(hwnd, WM_COMMAND, 4, 0);
break;
case '5':
SendMessage(hwnd, WM_COMMAND, 5, 0);
break;
case '6':
SendMessage(hwnd, WM_COMMAND, 6, 0);
break;
case '*':
SendMessage(hwnd, WM_COMMAND, 7, 0);
break;
case '1':
SendMessage(hwnd, WM_COMMAND, 8, 0);
break;
case '2':
SendMessage(hwnd, WM_COMMAND, 9, 0);
break;
case '3':
SendMessage(hwnd, WM_COMMAND, 10, 0);
break;
case '-':
SendMessage(hwnd, WM_COMMAND, 11, 0);
break;
case '0':
SendMessage(hwnd, WM_COMMAND, 12, 0);
break;
case '.':
SendMessage(hwnd, WM_COMMAND, 14, 0);
break;
case '+':
SendMessage(hwnd, WM_COMMAND, 15, 0);
break;
case '\r':
case '=':
SendMessage(hwnd, WM_COMMAND, 18, 0);
break;
default:
break;
}
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
|