#include <windows.h>
#define DIVISIONS 5
TCHAR szChildClass[] = TEXT("CHECKER3_CHILD"); //子窗口类名
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("My Windows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
//主窗口
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_ERROR);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
return 0;
}
//子窗口
wndclass.lpfnWndProc = ChildWndProc;
wndclass.cbWndExtra = sizeof(long); //功能彩蛋
wndclass.hIcon = NULL;
wndclass.lpszClassName = szChildClass;
RegisterClass(&wndclass);
hwnd = CreateWindow( //创建主窗口
szAppName,
TEXT("哈哈哈"),
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 HWND hwndChild[DIVISIONS][DIVISIONS];
int cxBlock,cyBlock,x,y;
switch(message)
{
case WM_CREATE:
for(x = 0;x < DIVISIONS;x++)
{
for(y = 0;y < DIVISIONS;y++)
{
hwndChild[x][y] = CreateWindow(
szChildClass, //
NULL, //标题
WS_CHILDWINDOW | WS_VISIBLE,//类型
0,0,0,0, //初始坐标、尺寸
hwnd, //父窗口句柄
(HMENU)(y << 8 | x),
//窗口菜单句柄(每个窗口唯一)
(HINSTANCE)GetWindowLongPtr(hwnd,GWL_HINSTANCE),
//窗口实例句柄
NULL); //创建参数
}
}
return 0;
case WM_SIZE:
cxBlock = LOWORD(lParam) / DIVISIONS;
cyBlock = HIWORD(lParam) / DIVISIONS;
for(x = 0;x < DIVISIONS;x++)
{
for(y = 0;y < DIVISIONS;y++)
{
MoveWindow(hwndChild[x][y],x * cxBlock,y * cyBlock,
cxBlock,cyBlock,TRUE); //修改窗口尺寸及位置
}
}
case WM_LBUTTONDOWN:
MessageBeep(0);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
}
LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(message)
{
case WM_CREATE:
SetWindowLongPtr(hwnd,0,0);
return 0;
case WM_LBUTTONDOWN:
SetWindowLongPtr(hwnd,0,1 ^ GetWindowLongPtr(hwnd,0));
InvalidateRect(hwnd,NULL,FALSE);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
Rectangle(hwnd,0,0,rect.right,rect.bottom);
if(GetWindowLongPtr(hwnd,0))
{
MoveToEx(hdc,0,0,NULL);
LineTo(hdc,rect.right,rect.bottom);
MoveToEx(hdc,0,rect.bottom,NULL);
LineTo(hdc,rect.right,0);
}
EndPaint(hwnd,&ps);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}