您的位置:首页 > 编程语言

[Windows] 双缓冲代码

2015-07-26 23:19 260 查看

双缓冲代码

使用本文的工具类,双缓冲使用起来更方便。

工具类的定义

工具类的使用

工具类的定义

CacheDrawUtility.h

#ifndef CACHE_DRAW_UTILITY
#define CACHE_DRAW_UTILITY

class CacheDrawUtility
{
public:
CacheDrawUtility(CDC* pDC, int nWidth, int nHeight, COLORREF bgColor);
~CacheDrawUtility();
CDC* GetDC() { return &m_memDC; }

private:
CDC* m_pDC;
int  m_nWidth;
int  m_nHeight;
COLORREF m_bgColor;
CDC  m_memDC;
CBitmap m_memBitmap;
};

#endif


CacheDrawUtility.cpp

#include "stdafx.h"
#include "CacheDrawUtility.h"

CacheDrawUtility::CacheDrawUtility(CDC* pDC, int nWidth, int nHeight, COLORREF bgColor)
{
m_pDC = pDC;
m_nWidth = nWidth;
m_nHeight = nHeight;
m_bgColor = bgColor;
m_memDC.CreateCompatibleDC(NULL);
m_memBitmap.CreateCompatibleBitmap(pDC, nWidth, nHeight);
m_memDC.SelectObject(&m_memBitmap); //将位图选入到内存设备描述表
m_memDC.FillSolidRect(0, 0, nWidth, nHeight, bgColor); // 用背景色将位图清除干净
}

CacheDrawUtility::~CacheDrawUtility()
{
m_pDC->BitBlt(0, 0, m_nWidth, m_nHeight, &m_memDC,0,0,SRCCOPY); // 将后备缓冲区中的图形拷贝到前端缓冲区
m_memBitmap.DeleteObject(); //绘图完成后的清理
m_memDC.DeleteDC();
}


工具类的使用

CDoubleCacheView.cpp

void CDoubleCacheView::OnDraw(CDC* pDC)
{
CDoubleCacheDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
{
// 使用双缓冲绘制
CRect rc;
GetClientRect(&rc);
CacheDrawUtility cacheDrawTool(pDC, rc.Width(), rc.Height(), RGB(255, 255, 255));
CDC* pNewDC = cacheDrawTool.GetDC();
for(int i=0; i<10000; i++)
{
pNewDC->MoveTo(rand()%1000, rand()%1000);
pNewDC->LineTo(rand()%1000, rand()%1000);
}

// 直接绘制
//      for(int i=0; i<10000; i++)
//      {
//          pDC->MoveTo(rand()%1000, rand()%1000);
//          pDC->LineTo(rand()%1000, rand()%1000);
//      }
}
}


[1]: /article/8512336.html MFC利用双缓冲实现屏幕无闪烁刷新
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: