【问题标题】:MFC: flickering issue with GDI+MFC:GDI+ 闪烁问题
【发布时间】:2015-04-12 06:24:11
【问题描述】:

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

我尝试使用双缓冲和擦除背景,我没有遇到闪烁问题,但我观察到绘图没有正确擦除。所以要擦除,我在 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;
}

如果我做错了,请告诉我。

【问题讨论】:

    标签: mfc


    【解决方案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;
    

    【讨论】:

    • 嗨,我需要使用 GDI_ 双缓冲。我使用它如下,但仍然是同样的问题。你能帮忙吗?位图缓冲区(circle.right, circle.bottom);图形 graphicsbuf(&buffer);图形图形(dc.m_hDC); graphicsbuf.SetSmoothingMode(SmoothingModeHighQuality); SolidBrush 笔刷(颜色(255,71,71,71));笔 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);
    • 你应该使用原始 GDI 东西进行双缓冲,但 GDI+ 用于实际绘图
    猜你喜欢
    • 2015-01-04
    • 2016-03-06
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 2018-08-04
    • 2017-02-01
    相关资源
    最近更新 更多