<Win32 API> GDI和GDI+ 双缓冲模式绘图

1. GDI双缓冲绘图

创建兼容DC, 在兼容DC上画好图后, 再通过BitBlt函数复制到所父DC.

void GdiDraw(HWND hwnd)
{
	RECT rc;
	GetClientRect(hwnd, &rc);
	
	rc.left += 25;
	rc.right -= 25;
	rc.top += 25;
	rc.bottom -= 25;

	int nWidth = rc.right - rc.left;
	int nHeight = rc.bottom - rc.top;

	wchar_t strText[] = TEXT("这是一个Gdi要显示的字符串");

	HDC hdc = GetDC(hwnd);
	SIZE sz;
	HDC memDc = CreateCompatibleDC(hdc);		

	HBITMAP memBmp = CreateCompatibleBitmap(memDc, nWidth, nHeight);
	HBITMAP oldBmp = (HBITMAP)SelectObject(memDc, memBmp);

	RECT fillRc;
	fillRc.left = 0;
	fillRc.top = 0;
	fillRc.bottom = nHeight;
	fillRc.right = nWidth;

	LOGBRUSH lb;
	lb.lbColor = RGB(255, 255, 255);
	lb.lbStyle = BS_SOLID;
	lb.lbHatch = 0;
	HBRUSH brush = CreateBrushIndirect(&lb);

	FillRect(memDc, &fillRc, brush);

	// 创建字体
	HGDIOBJ font = CreateFont(28, // nHeight 
		0, // nWidth 
		0, // nEscapement 
		0, // nOrientation 
		FW_NORMAL, // nWeight 
		FALSE, // bItalic 
		FALSE, // bUnderline 
		0, // cStrikeOut 
		DEFAULT_CHARSET, // nCharSet 
		OUT_DEFAULT_PRECIS, // nOutPrecision 
		CLIP_DEFAULT_PRECIS, // nClipPrecision 
		ANTIALIASED_QUALITY, // nQuality 
		DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily 
		TEXT("微软雅黑")); // lpszFac
	
	HGDIOBJ pen = ExtCreatePen(PS_SOLID, 1, &lb,  0, NULL);
	HGDIOBJ oldPen = (HGDIOBJ)SelectObject(memDc, pen);
	HGDIOBJ oldFont = (HGDIOBJ)SelectObject(memDc, font);
	

	::GetTextExtentPoint(memDc, strText, wcslen(strText), &sz);	// 获取文本显示所需的高度和宽度
	::SetTextColor(hdc, RGB(255, 0, 0));


	::DrawText(memDc, strText, wcslen(strText), &fillRc, DT_CENTER|DT_VCENTER|DT_SINGLELINE);	// 居中显示 

	BitBlt(hdc, rc.left, rc.top, nWidth, nHeight, memDc, 0, 0, SRCCOPY);

	SelectObject(memDc, oldBmp);
	SelectObject(memDc, oldPen);
	SelectObject(memDc, oldFont);
	DeleteObject(brush);
	DeleteObject(memDc);
	DeleteDC(hdc);
}

效果图示:




2. GDI+双缓冲绘图

创建Bitmap, Graphics::FromImg获取Graphics进行绘图, 再使用父Graphics进行DrawImg(Bitmap).

首先需要包含头文件:

#include <objidl.h>
#include <GdiPlus.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")

void GdiPlusDraw(HWND hwnd)
{
	RECT rc;
	GetClientRect(hwnd, &rc);

	rc.left += 25;
	rc.right -= 25;
	rc.top += 25;
	rc.bottom -= 25;

	int nWidth = rc.right - rc.left;
	int nHeight = rc.bottom - rc.top;
	Gdiplus::RectF gdiRc(rc.left, rc.top, nWidth, nHeight);

	HDC hdc = GetDC(hwnd);
	wchar_t strText[] = TEXT("这是一个GdiPlus要显示的字符串");

	Gdiplus::Graphics baseGraph(hdc);

	Gdiplus::Bitmap bmp(nWidth, nHeight);
	Gdiplus::Color white(0xFF, 0xFF, 0xFF);
	for (int x = 0; x < nWidth; x++)
	{
		for (int y = 0; y < nHeight; y++)
		{
			bmp.SetPixel(x, y, white);
		}
	}

	Gdiplus::Graphics *pGraphics = Graphics::FromImage(&bmp);

	Gdiplus::SolidBrush  solidbrush(Color(0xFF, 0x00, 0x00, 0xFF));		// 蓝色画刷
	Gdiplus::FontFamily  fontFamily(L"微软雅黑");
	Gdiplus::Font        gdifont(&fontFamily, 16, FontStyleRegular, UnitPixel);	// 字体
	Gdiplus::StringFormat sf;
	sf.SetAlignment(StringAlignmentCenter);
	sf.SetLineAlignment(StringAlignmentCenter);			// 文本居中显示

	pGraphics->SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
	pGraphics->SetTextRenderingHint(TextRenderingHintAntiAlias); // 设置平滑方式

	pGraphics->DrawString(strText, wcslen(strText), &gdifont, gdiRc, &sf, &solidbrush);

	baseGraph.DrawImage(&bmp, gdiRc, 0, 0, nWidth, nHeight, UnitPixel);

	DeleteDC(hdc);
}


参考链接: http://www.360doc.com/content/14/0516/14/13084517_378229356.shtml

http://www.cnblogs.com/wangjixianyun/archive/2012/12/18/2824264.html


  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值