windows作业 在屏幕画一个10*10的线

1  考虑的问题  如果直接画在屏幕 因为 擦除和重绘是需要耗费效率的,所以会亮瞎二十四kxx眼。为了解决这个问题 百度了一个概念双缓冲dc高端大气有没有 其实

所谓的双缓冲处理图像的过程,其实就是在一个兼容DC,以及一个兼容BITMAP上进行相关处理的过程,废话不多说上代码

因为很多的例子都是mfc 的 我写了一个 win32的    其中借鉴了一个大神的一篇文章 但是我现在找不到了。。希望大神不会介意


#include <windows.h>
#include <string>
#include <process.h>

bool g_recycling = false;//回收资源

void ShowError(char * strError)
{
	printf(strError, GetLastError());
	system("pause");
	exit(0);
}

unsigned _stdcall DrawingStart(void * lPraem)
{
	HWND  hDesktopHwnd = GetDesktopWindow();//获取屏幕HWND
	RECT rect;
	if (!GetWindowRect(hDesktopHwnd, &rect))//获取当前屏幕大小
		ShowError("GetWindowRect Error:[%d]");

	HDC hdc = GetDC(hDesktopHwnd);
	if (hdc ==NULL)
		ShowError("GetDC Error:[%d]");

	HDC hCompatibleDc = CreateCompatibleDC(hdc);//兼容DC
	if (!hCompatibleDc)
		ShowError("CreateCompatibleDC Error:[%d]");

	HBITMAP hBmp = CreateCompatibleBitmap(hdc, rect.right, rect.bottom);
	if (!hBmp)
		ShowError("CreateCompatibleBitmap Error:[%d]");

	HBITMAP hOldBmp = (HBITMAP)SelectObject(hCompatibleDc, hBmp);
	if (hOldBmp == NULL || hOldBmp == HGDI_ERROR)
		ShowError("SelectObject Error:[%d]");
	int x = rect.right / 10;
	int y = rect.bottom / 10;
	while (true)
	{

		for (int i = 0; i < 10; i++)
		{
			//画 横线
			if (!BitBlt(hdc, 0, y*i, rect.right, 1, hCompatibleDc, 0, 0, SRCCOPY))
				ShowError("BitBlt Error:[%d]");
			//画 竖线
			if (!BitBlt(hdc, x*i, 0, 1, rect.bottom, hCompatibleDc, 0, 0, SRCCOPY))
				ShowError("BitBlt Error:[%d]");
		}
		Sleep(10);
		if (g_recycling)
			break;
	}

	SelectObject(hCompatibleDc, hOldBmp);
	DeleteObject(hBmp);
	DeleteObject(hCompatibleDc);
	DeleteObject(hdc);
	return  0;
}

int _tmain(int argc, _TCHAR* argv[])
{

	HANDLE hThread = (HANDLE)_beginthreadex(nullptr, 0, DrawingStart, 0, 0, nullptr);
	if (hThread)
	{
		CloseHandle(hThread);
	}
	else
	{
		printf("启动失败");
		return 0;
	}
	printf("退出按任意键!!!\n");
	system("pause");
	g_recycling = true;
	Sleep(500);
	return 0;
}


猜你喜欢

转载自blog.csdn.net/w739639550/article/details/77532996
今日推荐