1. 程式人生 > >windows作業 在螢幕畫一個10*10的線

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;
}