Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double Buffering? Win32 c++

Tags:

c++

gdi

win32gui

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);
        }
like image 772
user1788175 Avatar asked Jan 04 '13 08:01

user1788175


1 Answers

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.

like image 59
user1788175 Avatar answered Nov 08 '22 07:11

user1788175