#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow) {
static TCHAR szAppName[] = TEXT("HIT TEST");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.lpfnWndProc = WndProc;
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);
}
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam) {
static int cxClient, cyClient, cxBlock, cyBlock, state[6][6];
HDC hdc;
int i, j, x, y;
PAINTSTRUCT ps;
static POINT pt;
switch(message) {
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
cxBlock = cxClient / 6;
cyBlock = cyClient / 6;
return 0;
case WM_KEYDOWN:
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
x = max(0, min(5, pt.x / cxBlock));
y = max(0, min(5, pt.y / cyBlock));
switch(wParam) {
case VK_UP:
y--;
break;
case VK_DOWN:
y++;
break;
case VK_LEFT:
x--;
break;
case VK_RIGHT:
x++;
break;
case VK_HOME:
x = 0;
break;
case VK_END:
x = 5;
break;
case VK_RETURN:
SendMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON,
MAKELONG(cxBlock * x, cyBlock * y));
break;
}
x = (x + 6) % 6;
y = (y + 6) % 6;
pt.x = cxBlock * (x + 0.5);
pt.y = cyBlock * (y + 0.5);
ClientToScreen(hwnd, &pt);
SetCursorPos(pt.x, pt.y);
return 0;
case WM_LBUTTONDOWN:
x = LOWORD(lParam) / cxBlock;
y = HIWORD(lParam) / cyBlock;
if(x < 6 && y < 6) {
state[x][y] ^= 1;
InvalidateRect(hwnd, NULL, FALSE);
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
for(i = 0; i < 6; i++)
for(j = 0; j < 6; j++) {
Rectangle(hdc, i * cxBlock, j * cyBlock,
(i + 1) * cxBlock, (j + 1) * cyBlock);
if(state[i][j]) {
MoveToEx(hdc, i * cxBlock, j * cyBlock, NULL);
LineTo(hdc, (i + 1) * cxBlock, (j + 1) * cyBlock);
MoveToEx(hdc, (i + 1) * cxBlock, j * cyBlock, NULL);
LineTo(hdc, i * cxBlock, (j + 1) * cyBlock);
}
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
|