#include <windows.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("buttonDemo");
WNDCLASS wndclass;
HWND hwnd;
MSG msg;
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 = GetStockObject(WHITE_BRUSH);
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,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
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 int cxClient, cyClient, cxChar, cyChar;
static HWND hwndButton1, hwndButton2, hwndCheckBox, hwndRadioButton,
hwndAutoCheckBox, hwndAutoRadioButton;
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hBrush;
RECT rect;
switch(message) {
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());
hwndButton1 = CreateWindow(TEXT("button"),
TEXT("button1"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, 0, 0,
hwnd,
(HMENU)1,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
hwndButton2 = CreateWindow(TEXT("button"),
TEXT("button2"),
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
0, 0, 0, 0,
hwnd,
(HMENU)2,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
hwndCheckBox = CreateWindow(TEXT("button"),
TEXT("CheckBox"),
WS_CHILD | WS_VISIBLE | BS_CHECKBOX,
0, 0, 0, 0,
hwnd,
(HMENU)3,
hInst,
NULL);
hwndRadioButton = CreateWindow(TEXT("button"),
TEXT("RadioButton"),
WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
0, 0, 0, 0,
hwnd,
(HMENU)4,
hInst,
NULL);
hwndAutoCheckBox = CreateWindow(TEXT("button"),
TEXT("AutoCheckBox"),
WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
0, 0, 0, 0,
hwnd,
(HMENU)5,
hInst,
NULL);
hwndAutoRadioButton = CreateWindow(TEXT("button"),
TEXT("AutoRadioButton"),
WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
0, 0, 0, 0,
hwnd,
(HMENU)6,
hInst,
NULL);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
MoveWindow(hwndButton1, cxChar, cyChar, 20 * cxChar, cyChar * 7 / 4, TRUE);
MoveWindow(hwndButton2, cxChar, 3 * cyChar, 20 * cxChar, cyChar * 7 / 4, TRUE);
MoveWindow(hwndCheckBox, cxChar, 5 * cyChar, 20 * cxChar, cyChar * 7 / 4, TRUE);
MoveWindow(hwndRadioButton, cxChar, 7 * cyChar, 20 * cxChar, cyChar * 7 / 4, TRUE);
MoveWindow(hwndAutoCheckBox, cxChar, cyChar * 9, 20 * cxChar, cyChar * 7 / 4, TRUE);
MoveWindow(hwndAutoRadioButton, cxChar, cyChar * 11, 20 * cxChar, cyChar * 7 / 4, TRUE);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam)) {
case 1:
MessageBeep(-1);
InvalidateRect(hwnd, NULL, TRUE);
break;
case 2:
MessageBeep(MB_ICONEXCLAMATION);
InvalidateRect(hwnd, NULL, TRUE);
break;
case 3:
MessageBeep(MB_ICONERROR);
InvalidateRect(hwnd, NULL, TRUE);
break;
case 4:
MessageBeep(MB_ICONINFORMATION);
InvalidateRect(hwnd, NULL, TRUE);
break;
case 5:
MessageBeep(MB_ICONSTOP);
InvalidateRect(hwnd, NULL, TRUE);
break;
case 6:
MessageBeep(MB_ICONWARNING);
InvalidateRect(hwnd, NULL, TRUE);
break;
default:
break;
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hBrush = CreateSolidBrush(RGB(rand() % 255, rand() % 255, rand() % 255));
GetClientRect(hwnd, &rect);
FillRect(hdc, &rect, hBrush);
EndPaint(hwnd, &ps);
DeleteObject(hBrush);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
|