#include<windows.h>
#include<windowsx.h>
#include<strsafe.h>
//每行和每列格子数
#define LINE_COLUMN_BLOCK_COUNT 5
bool Range(int r, int min, int max);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM);
TCHAR* ChildWndClsName = (TCHAR*)TEXT("CHILD__");
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
TCHAR* szAppName = (TCHAR*)TEXT("MyAppWindows");
TCHAR* ChildWndClsName = (TCHAR*)TEXT("CHILD__");
HWND hwnd = NULL;
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
if (!RegisterClass(&wc))
{
MessageBox(NULL, TEXT("Need Windows NT!!!!"), TEXT("Error"), MB_OK);
}
wc.lpfnWndProc = ChildWndProc;
wc.cbWndExtra =sizeof(long);
wc.hIcon = NULL;
wc.lpszClassName = ChildWndClsName;
RegisterClass(&wc);
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[LINE_COLUMN_BLOCK_COUNT][LINE_COLUMN_BLOCK_COUNT];
static int cxBlock, cyBlock, xIndex, yIndex;
static int cxClient, cyClient;
switch (message)
{
case WM_CREATE:
for (size_t i = 0; i < LINE_COLUMN_BLOCK_COUNT; i++)
{
for (size_t j = 0; j < LINE_COLUMN_BLOCK_COUNT; j++)
{
hwndChild[i][j] = CreateWindow(ChildWndClsName, NULL,
WS_CHILD | WS_VISIBLE,
0,
0,
0,
0,
hwnd,
(HMENU)(j << 8 | i),
(HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
NULL);
}
}
return 0;
case WM_SIZE:
cxBlock = LOWORD(lparam) / LINE_COLUMN_BLOCK_COUNT;
cyBlock = HIWORD(lparam) / LINE_COLUMN_BLOCK_COUNT;
for (size_t i = 0; i < LINE_COLUMN_BLOCK_COUNT; i++)
{
for (size_t j = 0; j < LINE_COLUMN_BLOCK_COUNT; j++)
{
MoveWindow(hwndChild[i][j], i * cxBlock, j * cyBlock, cxBlock, cyBlock, TRUE);
}
}
return 0;
case WM_LBUTTONDOWN:
MessageBeep(0);
return 0;
case WM_DESTROY://接受WM_DESTROY消息并发送WM_QUIT消息
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static int t,t0;
switch (message)
{
case WM_CREATE:
SetWindowLongPtr(hwnd, 0, 0);
hdc = GetDC(hwnd);
TextOut(hdc, 0, 0, TEXT("a"), 1);
ReleaseDC(hwnd, hdc);
return 0;
case WM_LBUTTONDOWN:
SetWindowLongPtr(hwnd, 0, (t0=!GetWindowLongPtr(hwnd, 0)));//在调试里面,这里t0明明是1
hdc = GetDC(hwnd);
t = GetWindowLongPtr(hwnd, 0);//但是在调试里面这里再获取的时候却莫名其妙变成0了???而且t0好像销毁不见了??
ReleaseDC(hwnd, hdc);
InvalidateRect(hwnd, NULL, FALSE);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
//画个矩形
Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
//画两条对角线
if (GetWindowLongPtr(hwnd, 0))
{
MoveToEx(hdc, 0, 0, NULL);
LineTo(hdc, rect.right, rect.bottom);
MoveToEx(hdc, rect.right, 0, NULL);
LineTo(hdc, 0, rect.bottom);
}
EndPaint(hwnd, &ps);
return 0;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
请问为什么点击窗口没有反应?还有我在注释里面的两行问题,有没有大佬帮忙解答一下