减少阻力和掉落闪烁

时间:2014-01-23 12:50:17

标签: visual-studio-2010 visual-c++ mfc drag-and-drop flicker

我的代码出了问题。我为Drag&创建了一个类掉落但是在拖动对象的过程中,我看到了拖动对象的恼人的闪烁效果

void CDragDropListBox::DrawDragRect(CPoint point, CDragItem* DragItem,SIZE Size)
{
    CDC* pDC = GetDC();
    ScreenToClient(&point);

    //Rect centered under mouse pointer
    point.x -= Size.cx/2;
    point.y -= Size.cy/2;

    CRect rectFull(point,m_RectSize);

    //Delete previous rect
    ClientToScreen(&m_OldRect);
    _DstWnd->ScreenToClient(&m_OldRect);
    _DstWnd->InvalidateRect(m_OldRect, true);
    _DstWnd->UpdateWindow();

    //Draw new rect based on mouse position
    DrawSelectFrame(pDC,rectFull);
    DrawSingleItem(DragItem,pDC,rectFull);

    m_OldRect = rectFull;
}

在我的代码中,每当我移动鼠标时,我都会删除之前绘制的拖动矩形并绘制一个新的,但闪烁是非常挑剔的...... 我能做什么?

2 个答案:

答案 0 :(得分:0)

这里的主要问题是,擦除和绘画分两步完成。 即使它执行得很快。它看起来像闪烁。

所以不要使用WM_ERASEBKGND,并在WM_PAINT中使用双缓冲(带内存DC)。

请参阅Codeproject中的CMemDC,以获得简单易用的类。

答案 1 :(得分:0)

我编辑了我的代码以删除UpdateWindow()的使用

   void CDragDropListBox::DrawDragRect(CPoint point, CDragItem* DragItem,SIZE Size)
{
    CDC* pDC = _DstWnd->GetDC();
    CDC dcMemory;

    ScreenToClient(&point);
    dcMemory.CreateCompatibleDC(pDC);

    CDC* olddc= pDC;

    CRect tmprect;
    pDC->GetClipBox(&tmprect);

    CBitmap tmpbmp;
    tmpbmp.CreateCompatibleBitmap(pDC, tmprect.Width(), tmprect.Height());

    CBitmap* OldBmp;
    OldBmp = dcMemory.SelectObject(&tmpbmp);

    point.x -= Size.cx/2;
    point.y -= Size.cy/2;

    CRect rectFull(point,m_RectSize);

    ClientToScreen(&m_OldRect);
    _DstWnd->ScreenToClient(&m_OldRect);
    _DstWnd->InvalidateRect(m_OldRect, TRUE);

    m_OldRect = rectFull;

    ClientToScreen(&rectFull);
    _DstWnd->ScreenToClient(&rectFull);
    DrawSelectFrame(pDC,rectFull);
    DrawSingleItem(DragItem,pDC,rectFull);
    _DstWnd->ValidateRect(rectFull);

    //_DstWnd->UpdateWindow();

    olddc->BitBlt(tmprect.left, tmprect.top, tmprect.Width(), tmprect.Height(), pDC, tmprect.left, tmprect.top, SRCCOPY);

    dcMemory.SelectObject(OldBmp);

}

闪烁少,但我可以进一步改善吗?