拖动窗口一角时客户区出现残影

StreamingMoon 40 信誉分
2025-06-15T13:21:53.5966667+00:00

我编了一个在客户区里生成一个迷宫的程序

User's image

当我拖动窗口的左上角时就会有这样的事情

f6a02127e17665bd99aef052c6bb912d

这是个Win32窗口程序,以下是部分代码:

void Maze();
LRESULT WndProc(
	HWND hWnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam
);
int WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR     lpCmdLine,
	int       nShowCmd
)
{
	Maze();
	const wchar_t* class_name = L"Maze";
	WNDCLASS wc = {};
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = class_name;
	if (!RegisterClass(&wc))
	{
		MessageBox(NULL, L"Failed to register window class!", L"Error", 0);
	}
	RECT rect = { 0,0,400,400 };
	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);
	HWND h = CreateWindowEx(
		NULL,
		class_name,
		L"Maze",
		WS_OVERLAPPEDWINDOW,
		0, 0, rect.right-rect.left, rect.bottom-rect.top,
		NULL,
		NULL,
		hInstance,
		NULL
	);
	if (!h)
	{
		MessageBox(NULL, L"Failed to register window class!", L"Error", 0);
	}
	else
	{
		ShowWindow(h, nShowCmd);
	}
	MSG msg = { };
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}
LRESULT WndProc(
	HWND hWnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam
)
{
	switch (uMsg)
	{
	case WM_PAINT:
	{
		PAINTSTRUCT ps;
		HDC hdc = BeginPaint(hWnd, &ps);
		HBRUSH hBlankBrush = CreateSolidBrush(RGB(0, 0, 0));
		HBRUSH hWallBrush = CreateSolidBrush(RGB(255, 255, 255));
		HBRUSH hCharacterBrush = CreateSolidBrush(RGB(255, 0, 0));
		for (int i = 0; i < 20; i++)
		{
			for (int j = 0; j < 20; j++)
			{
				RECT rect = { 20 * i, 20 * j, 20 * (i + 1) + 1, 20 * (j + 1) + 1 };
				switch (grid[i][j])
				{
				case Blank:
					FillRect(hdc,&rect, hBlankBrush);
					break;
				case Wall:
					FillRect(hdc,&rect, hWallBrush);
					break;
				case At:
					FillRect(hdc,&rect, hCharacterBrush);
					break;
				}
				
			}
		}
		DeleteObject(hBlankBrush);
		DeleteObject(hWallBrush);
		DeleteObject(hCharacterBrush);
		EndPaint(hWnd, &ps);
		return 0;
	}
	case WM_CLOSE:
		DestroyWindow(hWnd);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	default:
		return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}
}
void Maze()
{
...
}

Maze()是生成迷宫的函数,我觉得它与这个现象无关,所以没有贴上代码

我想知道为什么会这样

开发人员技术 | C++
0 个注释 无注释
{count} 票

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。