双缓冲为什么应该这样用?

来源:互联网 发布:带-域名 编辑:程序博客网 时间:2024/06/01 21:07

看了很多的使用双缓冲的例子。有的时候也知道是应该那样用,原理也知道一点,但就是写不出来,为何?

脑子里面没正确的概念,有的只是个皮毛,现在虽然不完全明白。仅就把所知道的写下来,与诸君一同学习。

先上代码:

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 usingCreateCompatibleBitmap 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 theGetDeviceCaps function.

When you no longer need the memory device context, call the DeleteDC function to delete it. 



原创粉丝点击