MFC:GDI +的闪烁问题

时间:2015-04-12 06:24:12

标签: mfc

我创建了一个示例对话框应用程序,其中绘制了一个圆圈。同样在鼠标移动时,将重新绘制圆圈。我在下面提供我的代码。它也是可编辑的。

我尝试使用双缓冲和erasebackground,我没有得到闪烁的问题,但我发现绘制没有正确删除。所以为了擦除,在OnPaint中我编写了擦除代码。我再次面临着闪烁的问题。

void CPOCDlg::OnPaint()
{
    CPaintDC dc(this);
    GetClientRect(&clientRect);


    circle = clientRect;
    circle.DeflateRect(100,100);
    dc.SelectStockObject(NULL_BRUSH);
    dc.SelectStockObject(NULL_PEN);
    dc.FillSolidRect(circle, ::GetSysColor(COLOR_BTNFACE));

    Bitmap buffer(circle.right, circle.bottom);
    Graphics graphicsbuf(&buffer);
    Graphics graphics(dc.m_hDC);
    graphicsbuf.SetSmoothingMode(SmoothingModeHighQuality);

    SolidBrush brush(Color(255,71,71,71));
    Pen bluePen(Color(255, 0, 0, 255),1);

    graphicsbuf.DrawEllipse(&bluePen,Rect(circle.left,circle.top,circle.Width(),circle.Height()));
    graphicsbuf.SetSmoothingMode(SmoothingModeHighQuality);
    graphics.DrawImage(&buffer, 0, 0);

}

void CPOCDlg::OnMouseMove(UINT nFlags, CPoint point)
{
    m_point = point;
    InvalidateRect(circle,FALSE);

    CDialogEx::OnMouseMove(nFlags, point);
}


BOOL CPOCDlg::OnEraseBkgnd(CDC* pDC)
{
    return TRUE;
}

如果我有任何错误,请告诉我。

1 个答案:

答案 0 :(得分:1)

您需要使用所谓的双缓冲技术来防止闪烁:

// create Mem DC
dcMemory = new CDC;
dcMemory->CreateCompatibleDC(pDC);
pDC->SetMapMode(MM_TEXT);
dcMemory->SetMapMode(MM_TEXT);

// TODO: draw to memDC here

//switch back to paint dc
pDC->BitBlt(rectDirty.left, rectDirty.top, 
rectDirty.Width(), rectDirty.Height(), 
dcMemory, 
rectDirty.left,rectDirty.top,SRCCOPY);

dcMemory->DeleteDC();
delete dcMemory;
dcMemory = NULL;