本文使用的技术均来自卡里象棋
1.1. 缓冲消除闪烁(MFC版本)
先做一个内存dc,将背景绘制到这个dc,再将棋盘绘制到这个dc,再将棋子绘制到这个dc.
此时dc里面就是一个累积的完整的最终效果图。此时再把这个dc拷贝到当前绘图句柄上,就等于只跟上一次图相比,只有局部内容发生的变化,而且是内存拷贝,所以很快就绘制了一个变化之后的图。不会有闪烁。
void CchessDlg::PaintChessboard(void)
{
BITMAP bmpSize;
m_chessBoardBitmap.GetBitmap(&bmpSize);//获取位图的大小等信息
CDC memChessBoardDC;
memChessBoardDC.CreateCompatibleDC(&m_dcCompatible);
memChessBoardDC.SelectObject(&m_chessBoardBitmap);
//画棋盘
CRect rect(30+10,30+10,61*9+15,61*10+15);
m_dcCompatible.StretchBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memChessBoardDC, 0, 0, bmpSize.bmWidth,bmpSize.bmHeight, SRCCOPY);
memChessBoardDC.DeleteDC();
}
void CchessDlg::OnPaint()
{
InitDCInMemory();
PaintBack();
PaintChessboard();
PaintSourceChessMan();
PaintChessman();
//将上面的图片一次性绘制到当前DC中,消除闪烁
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.BitBlt(0, 0, rect.Width(), rect.Height(), &m_dcCompatible, 0, 0, SRCCOPY);
CDialog::OnPaint();
}
1.2. 缓冲消除闪烁(GDI版本)
思路是同样的,就是将多个图形先一次绘制到内存中,最后一次性拷贝整个内存图片到当前屏幕dc.
void CchessDlg::OnPaint()
{
CPaintDC dc(this);
Gdiplus::Graphics graphics(dc);
Gdiplus::Rect rectGdi;
graphics.GetVisibleClipBounds(&rectGdi);
//创建缓冲区
Gdiplus::Bitmap memBitmap(rectGdi.Width, rectGdi.Height);
Gdiplus::Graphics* pGraphicsMemBmp = Gdiplus::Graphics::FromImage(&memBitmap);
PaintBack(*pGraphicsMemBmp);
PaintChessboard(*pGraphicsMemBmp);
PaintSourceChessMan(*pGraphicsMemBmp);
PaintChessman(*pGraphicsMemBmp);
graphics.DrawImage(&memBitmap, rectGdi);
delete pGraphicsMemBmp;
CDialog::OnPaint();
}