天天看点

Gdi+绘图2——使用双缓冲解决闪烁问题

使用Gdi+绘图后,出现很明显的闪烁问题,于是使用Gdi+的Bitmap来进行双缓冲解决问题。

void CLedButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	// 获取按钮矩形区域
	CRect tempRect = lpDrawItemStruct->rcItem;

	// 建立缓冲使用的位图
	Bitmap buffer(tempRect.Width(), tempRect.Height());
	// 建立Graphics对象
	Graphics graphics(&buffer);

	// 填充椭圆,外部边缘是 173灰-255白
	LinearGradientBrush brush1(Point(0,0), Point(0,tempRect.Height()), COLOR_EDGEGRAY, COLOR_WHITE);
	graphics.FillEllipse(&brush1, tempRect.left, tempRect.top, tempRect.Width(), tempRect.Height());

	// 位图显示
	Graphics g2(lpDrawItemStruct->hDC); 
	g2.DrawImage(&buffer, 0, 0);
}
           

先在Bitmap上使用Graphics绘制画面,绘制完成后,再使用新的Graphics将Bitmap显示在窗口上。