VC使用雙緩沖避免繪圖閃爍的正確使用方法【轉】
使用內存DC繪圖,然后實現雙緩沖,避免繪圖閃爍,這個小技術簡單但很有效。但是仍然有很多人說使用了雙緩沖,圖片卻仍然有閃爍,分析了幾個這樣的例子,發現
其實不是雙緩沖的技術問題,而是使用者沒有正確理解和使用雙緩沖的方法。使用雙緩沖要點如下:
1. 保證繪圖過程中的所有CDC及其繼承類指向內存DC。
在窗口或者視圖中繪圖,一般都是在OnDraw或者OnPaint事件中,但是有時根據需要繪圖是通過調用其他類及函數完成比較復雜的繪制,在這些函數中,有時編寫者會獲取諸如CClientDC,然后繪圖,此時的任何動作都會繞過緩沖區直接繪制到屏幕,從而造成閃爍。正確的做法是檢查并修改所有繪圖過程函數,避免直接獲取CClientDC、CWindowDC、CPaintDC之類。而是采用傳遞CDC指針的方式寫繪圖類或者函數。
2. 修改OnEraseBkgnd(CDC* /*pDC*/) 事件
將代碼屏蔽,改為一句 return TRUE; 這樣做是避免使用原來父類代碼中的擦除屏幕語句。
3. 另一個容易忽略的關鍵點-〉擦除背景。
第2條是必要的,避免了擦除背景的工作,但是這不代表背景不需要擦除了,只不過這個擦除過程要放到內存緩沖區中去做。
例如下面代碼:
void
CGraphView::EraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CRect rect;
GetClientRect( &rect );
CBrush brush;
brush.CreateSolidBrush(GetColor(CColorClass::clrGraphBK) );
pDC->FillRect( &rect, &brush );
}
void
CGraphView::OnDraw(CDC* pDC)
{
CRect rectClient;
GetClientRect( &rectClient );
CMemDC memDC(pDC, rectClient);
EraseBkgnd(&memDC);
// OnEraseBkgnd 失效了,但是仍然需要在內存緩沖區中擦除背景
m_graph.Redraw( &memDC, rectClient );
}
如果要求更高的繪圖效率,重畫時可以采用局部擦除的辦法,即擦除一定區域內的代碼。
使用雙緩沖的整個步驟如下:
定義內存設備CMemDC,將所有繪圖DC指向該設備 ---〉去掉擦除背景語句 ---〉在內存DC中擦除背景
-〉在內存DC中繪圖 -〉結果切換到顯示DC。
實際應用于復雜圖形繪制,沒有任何閃爍變化。
*文中提到的雙緩沖代碼CMemDC是個開源類,其內容如下:
#ifndef _MEMDC_H_
#define _MEMDC_H_
//////////////////////////////////////////////////
// CMemDC - memory DC
//
// Author: Keith Rule
// Email: keithr@europa.com
// Copyright 1996-1999, Keith Rule
//
// You may freely use or modify this code provided this
// Copyright is included in all derived versions.
//
// History - 10/3/97 Fixed scrolling bug.
// Added print support. - KR
//
// 11/3/99 Fixed most common complaint. Added
// background color fill. - KR
//
// 11/3/99 Added support for mapping modes other than
// MM_TEXT as suggested by Lee Sang Hun. - KR
//
// This class implements a memory Device Context which allows
// flicker free drawing.
class
CMemDC :
public
CDC {
protected
:
CBitmap m_bitmap;
// Offscreen bitmap
CBitmap* m_oldBitmap;
// bitmap originally found in CMemDC
CDC* m_pDC;
// Saves CDC passed in constructor
CRect m_rect;
// Rectangle of drawing area.
BOOL
m_bMemDC;
// TRUE if CDC really is a Memory DC.
void
Construct(CDC* pDC)
{
ASSERT(pDC != NULL);
// Some initialization
m_pDC = pDC;
m_oldBitmap = NULL;
m_bMemDC = !pDC->IsPrinting();
if
(m_bMemDC) {
// Create a Memory DC
CreateCompatibleDC(pDC);
pDC->LPtoDP(&m_rect);
m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_oldBitmap = SelectObject(&m_bitmap);
SetMapMode(pDC->GetMapMode());
pDC->DPtoLP(&m_rect);
SetWindowOrg(m_rect.left, m_rect.top);
}
else
{
// Make a copy of the relevent parts of the current DC for printing
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
}
// Fill background
FillSolidRect(m_rect, pDC->GetBkColor());
}
// TRK begin
public
:
CMemDC(CDC* pDC ) : CDC() { pDC->GetClipBox(&m_rect); Construct(pDC); }
CMemDC(CDC* pDC,
const
RECT& rect) : CDC() { m_rect = rect ; Construct(pDC); }
// TRK end
virtual
~CMemDC()
{
if
(m_bMemDC) {
// Copy the offscreen bitmap onto the screen.
m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
this
, m_rect.left, m_rect.top, SRCCOPY);
//Swap back the original bitmap.
SelectObject(m_oldBitmap);
}
else
{
// All we need to do is replace the DC with an illegal value,
// this keeps us from accidently deleting the handles associated with
// the CDC that was passed to the constructor.
m_hDC = m_hAttribDC = NULL;
}
}
// Allow usage as a pointer
CMemDC* operator->()
{
return
this
;
}
// Allow usage as a pointer
operator CMemDC*()
{
return
this
;
}
};
#endif
原文鏈接:http://blog.csdn.net/r3000/article/details/5454262
RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成