看了很多的使用雙緩衝的例子。有的時候也知道是應該那樣用,原理也知道一點,但就是寫不出來,為何?
腦子裡面沒正確的概念,有的只是個皮毛,現在雖然不完全明白。僅就把所知道的寫下來,與諸君一同學習。
先上代碼:
void CXXXWnd::OnPaint(){CPaintDC dc(this);CDC memDC;memDC.CreateCompatibleDC(&dc);CRect rect;GetClientRect(rect);CBitmap bm;bm.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());memDC.SelectObject(&bm);memDC.SetBkMode(TRANSPARENT);memDC.FillSolidRect(rect, RGB(128,128,0));dc.BitBlt(0,0,rect.Width(),rect.Height(), &memDC, 0, 0, SRCCOPY);}
雙緩衝其實現在記憶體畫圖,那麼我們需要一個記憶體DC。
這裡定義為CDC memDC,並建立相容DC。
同理也建立一個相容源DC的CBitmap。
在記憶體DC畫圖,將該DC內容畫到實際DC上。
借鑒MSDN雙緩衝步驟:
Remarks
A memory device context is a device context that exists only in memory. When the memory device context is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high.
Before an application can use a memory device context for drawing operations, it must select a bitmap of the correct width and height into the device context. This may be done by using
CreateCompatibleBitmap to specify the height, width, and color organization required in the function call.
When a memory device context is created, all attributes are set to typical default values. The memory device context can be use as a typical device context. You can set the attributes to non-default values, obtain the current setting of its attributes, and
select pens, brushes and regions into it.
The CreateCompatibleDC function can only be used with devices that support raster operations. An application can determine whether a device supports these operations by calling the
GetDeviceCaps function.
When you no longer need the memory device context, call the DeleteDC function to delete it.