Double Buffering? Win32 c++

谁说我不能喝 提交于 2019-12-07 00:10:36

问题


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:


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.



来源:https://stackoverflow.com/questions/14153387/double-buffering-win32-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!