[MFC]OnPaint双缓冲绘图

void CXButton::OnPaint()
{
	CRect PaintRect;
	CPaintDC dc(this);
	CDC MemDC;
	CBitmap MemBitmap;

	// 双缓冲绘图
	GetClientRect(PaintRect);
	if (MemDC.CreateCompatibleDC(&dc))
	{
		if (MemBitmap.CreateCompatibleBitmap(&dc, PaintRect.Width(), PaintRect.Height()))
		{
			if (HGDIOBJ OldObject = MemDC.SelectObject(MemBitmap))
			{
				// 拷贝背景图, 以处理透明色
				if (MemDC.BitBlt(0, 0, PaintRect.Width(), PaintRect.Height(), &dc, 0, 0, SRCCOPY))
				{
					Gdiplus::Graphics g(MemDC);

					// Paint here ...

					dc.BitBlt(0, 0, PaintRect.Width(), PaintRect.Height(), &MemDC, 0, 0, SRCCOPY);
				}
				MemDC.SelectObject(OldObject);
			}
			MemBitmap.DeleteObject();
		}
		MemDC.DeleteDC();
	}
}

你可能感兴趣的:([MFC]OnPaint双缓冲绘图)