Double buffering? Win32 C ++

I am trying to implement double buffering, but it does not seem to work, i.e. the graphics are still flickering.

WM_PAINT is called every time the mouse moves. (WM_MOUSEMOVE)

Insert WM_PAINT below:

case WM_PAINT: { hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... RECT rect; GetClientRect(hWnd, &rect); int width=rect.right; int height=rect.bottom; HDC backbuffDC = CreateCompatibleDC(hdc); HBITMAP backbuffer = CreateCompatibleBitmap( hdc, width, height); int savedDC = SaveDC(backbuffDC); SelectObject( backbuffDC, backbuffer ); HBRUSH hBrush = CreateSolidBrush(RGB(255,255,255)); FillRect(backbuffDC,&rect,hBrush); DeleteObject(hBrush); if(fileImport) { importFile(backbuffDC); } if(renderWiredCube) { wireframeCube(backbuffDC); } if(renderColoredCube) { renderColorCube(backbuffDC); } BitBlt(hdc,0,0,width,height,backbuffDC,0,0,SRCCOPY); RestoreDC(backbuffDC,savedDC); DeleteObject(backbuffer); DeleteDC(backbuffDC); EndPaint(hWnd, &ps); } 
+7
source share
1 answer

Add the following handler:

 case WM_ERASEBKGND: return 1; 

The reason it works is because this message is sent before drawing to ensure that drawing is done against the background of the window. Blinking goes back and forth between the background and what is drawn on it. When the background ceases to be colored, it ceases to contradict what is painted on it, which includes filling the window with solid color, so there will still be a background.

+9
source

All Articles