9

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

每次鼠标移动时都会调用 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);
        }
4

1 回答 1

11

添加以下处理程序:

case WM_ERASEBKGND:
    return 1;

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

于 2013-01-04T08:14:45.263 回答