双缓冲? Win32 c ++

时间:2013-01-04 08:05:45

标签: c++ gdi win32gui

我正在尝试实现双缓冲,但它似乎不起作用,即图形仍然闪烁。

每次鼠标移动时都会调用WM_PAINT。 (WM_MOUSEMOVE)

粘贴下面的WM_PAINT:

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 个答案:

答案 0 :(得分:9)

添加以下处理程序:

case WM_ERASEBKGND:
    return 1;

它起作用的原因是因为在绘制之前发送此消息以确保在窗口类的背景上完成绘制。闪烁在背景和涂在它上面的东西之间来回传递。一旦背景停止被绘制,它就会停止与其上绘制的内容相冲突,其中包括用纯色填充窗口,因此仍然会有背景。