GDI+ 性能优化之双缓冲

来源:互联网 发布:移动领流量软件 编辑:程序博客网 时间:2024/05/14 09:05

1、使用CImage
void CXXXButton::OnPaint()
{
    CPaintDC dc(this);




    CRect      rect;
    this->GetClientRect(&rect);




    int imageW = m_pImage->GetWidth();          //m_pImage你自己的成员变量,自己去定义吧。
    int imageH = m_pImage->GetHeight();




    CImage memImg;                              //创建CImage缓存
    memImg.Create(rect.Width(), rect.Height(), m_pImage->GetBPP());
    HDC tmpdc = memImg.GetDC();
    CDC memDC;
    memDC.Attach(tmpdc);




    //使用 memDC 开始
    memDC.FillSolidRect(rect, RGB(255, 255, 255));
    m_pImage->Draw(tmpdc, 0, 0, imageW/m_nStates, imageH, imageW*0/m_nStates, 0, imageW/m_nStates, imageH);
    //使用 memDC 结束




    memImg.Draw(dc.m_hDC, 0, 0, rect.Width(), rect.Height());  //将CImage缓存贴到dc上




    memDC.Detach();           //释放这两个,不释放crash
    memImg.ReleaseDC();
}


2、使用位图
BOOL CXXXDlg::OnEraseBkgnd(CDC *pDC)
{
    CRect  rect;
    this->GetClientRect(rect);




    if(!m_pBkImage)                                                          //定义的一个成员变量
    {
        m_pBkImage = new Image(_T("main_frame.png"));
    }




    Bitmap bmp(rect.right, rect.bottom);




    Graphics bmpGraphics(&bmp);
    bmpGraphics.SetSmoothingMode(SmoothingModeAntiAlias);
    bmpGraphics.DrawImage(m_pBkImage, 0, 0, rect.Width(), rect.Height());    //Drawing on bitmap








    Graphics graphics(pDC->m_hDC);                                           //Drawing on DC




    CachedBitmap cachedBmp(&bmp,&graphics);
    graphics.DrawCachedBitmap(&cachedBmp, 0, m_iCaptionH);




    graphics.ReleaseHDC(pDC->m_hDC);




    return TRUE;
}




3、使用CacheBitmap
转载地址:http://cnbeta2004.blog.163.com/blog/static/602313402010119104451537/


早前曾为此问题在CSDN发帖求助(GDI+ 如何使用双缓冲绘制图像),得到了一个GDI+下较可行的方法,虽然绘制效果比直接绘制要好一些,不过还不能跟GDI的双缓冲方式比肩。


现在,我终于找到了一个理想的实现方式,效果与GDI的实现不相上下,代码如下:
[cpp] view plaincopy
/*C++ code*/  
RECT rc;  
GetClientRect(g_hwnd,&rc);  
Bitmap bmp(int(rc.right),int(rc.bottom));  
  
Graphics bmpGraphics(&bmp);  
bmpGraphics.SetSmoothingMode(SmoothingModeAntiAlias);  
  
/*Drawing on bitmap*/  
SolidBrush bkBrush(Color(0,0,0));  
bmpGraphics.FillRectangle(&bkBrush,0,0,rc.right,rc.bottom);  
  
/*Drawing on DC*/  
Graphics graphics(hdc);  
/*Important! Create a CacheBitmap object for quick drawing*/  
CachedBitmap cachedBmp(&bmp,&graphics);  
graphics.DrawCachedBitmap(&cachedBmp,0,0);  
以上的绘制代码最区别于网络上其他GDI+实现的一处就是,在最后添加了一个CacheBitmap对象用于快速绘制。


CacheBitmap是一个包含了bmp全部象素,并且针对graphics所关联的DC做过特别优化的位图对象。这点可以从其构造参数上看到。 


关于双缓冲的实现还有一点十分关键,虽然它不属于双缓冲实现的核心。如果绘制需要经常的重绘背景,则需要自己拦截WM_ERASEBKGND消息,并在处理函数中什么也不做,即此消息发生时不重画背景,背景的重画在WM_PAINT中全权控制。


如,WM_ERASEBKGND消息处理的实现
[cpp] view plaincopy
void OnEraseBkGnd(HDC hdc)  
{  
//do nothing  
}  
附:GDI的双缓冲实现
[cpp] view plaincopy
RECT rc;  
GetClientRect(hwnd,&rc);  
HDC hMemDc = CreateCompatibleDC(hdc);  
HBITMAP hBmp = CreateCompatibleBitmap(hdc,rc.right,rc.bottom);  
HBITMAP hOldBmp = (HBITMAP)SelectObject(hMemDc,hBmp);  
//在此使用hMemDc进行 GDI 绘制  
BitBlt(hdc,0,0,rc.right,rc.bottom,hMemDc,0,0,SRCCOPY);  
SelectObject(hMemDc,hOldBmp);  
DeleteObject(hBmp);  
DeleteObject(hMemDc);