通常在绘图的时候,你都不会直接在BeginPaint或CDC中使用HDC,如果你这样做了,那么当你绘制很多东西的时候,就有可能造成图像的闪烁。双缓冲就是用在这里实现图像的平滑的更新的。
有很多实现的方法,一种是在内存中创建一个兼容的HDC,然后拷贝内存到可见的HDC,另一种是我现在用的一个方法,使用一个DirectX的子集DirectDraw,实现一个接口的实例类CDXSurfaceMgr,实现双缓冲。
CDXSurfaceMgr creates a primary surface - one that is visible - and a secondary off screen surface that is the same size as the primary one but invisible. When you call BeginPaint an HDC attached to the secondary surface is returned; you do all your drawing into this; then EndPaint Blits(copies) the whole of the secondary surface onto the primary one and it appears on the window. If you have a capable graphics adapter on your machine then the Blit is very very fast, if you don't then the operation is obviously not as fast, but still very functional.