雙緩衝為什麼應該這樣用?

來源:互聯網
上載者:User

看了很多的使用雙緩衝的例子。有的時候也知道是應該那樣用,原理也知道一點,但就是寫不出來,為何?

腦子裡面沒正確的概念,有的只是個皮毛,現在雖然不完全明白。僅就把所知道的寫下來,與諸君一同學習。

先上代碼:

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. 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.