9
votes

I am trying to implement double buffering but it doesn't seem to work i.e. the graphic still flickers.

The WM_PAINT gets called everytime when the mouse moves. (WM_MOUSEMOVE)

Pasted 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);
        }
1

1 Answers

11
votes

Add the following handler:

case WM_ERASEBKGND:
    return 1;

The reason it works is because this message is sent before painting to ensure that painting is done on the window class's background. The flashing is going back and forth between the background and what's painted over it. Once the background has stopped being painted, it stops conflicting with what is painted over it, which includes filling the window with a solid colour, so there will still be a background anyway.