How do I prevent flickering on CListCtrl?

sorin picture sorin · Mar 21, 2010 · Viewed 7.3k times · Source

I'm using a CListCtrl/CListView report view (LVS_REPORT) in virtual mode (LVS_OWNERDATA) with LVS_EX_DOUBLEBUFFER enabled and I encounter ugly flickering. Double buffer have a real effect but it doesn't stop all flickering (without it very slow).

I'm not looking for switching to other controls that would require a high amount of rework (like ObjectListView)

How does the flickering behaves:

  • on column resize - the background is first clean using lightgray and after this is displayed the text (background is white)
  • on mouse scroll (animated) - for a very short time there is lightgray-bar displayed in the area where new lines are to be displayed.

It looks like it does clean the background using the default window background color (lightgray) for the area where it has to redraw.

How do I solve the flickering problem?

Answer

VitalyVal picture VitalyVal · Mar 21, 2010

Try to do the following: - Set Clip Children and Clip Sibling for paremt dialog of List Control. - Make dirived from CListCtrl class. In this class overwrite OnEraseBkgnd. In the OnEraseBkgnd fill with background color area around of visible items of the list. The OnEraseBkgnd can look like:

BOOL CListCtrlEx::OnEraseBkgnd(CDC* pDC) 
{
    CBrush  br;
    CRect   rcCli;
    CRect   rcItemsRect(0, 0, 0, 0);
    int     nHeadHeight = 0;
    int     nItems      = GetItemCount();

    GetClientRect(&rcCli);

    CHeaderCtrl* pHeadCtrl = GetHeaderCtrl();
    if (pHeadCtrl)
    {
        CRect  rcHead;
        pHeadCtrl->GetWindowRect(&rcHead);
        nHeadHeight = rcHead.Height();
    }
    rcCli.top += nHeadHeight;

    if (nItems > 0)
    {
        CPoint  ptItem;
        CRect   rcItem;

        GetItemRect(nItems - 1, &rcItem, LVIR_BOUNDS);
        GetItemPosition(nItems - 1, &ptItem);

        rcItemsRect.top    = rcCli.top;
        rcItemsRect.left   = ptItem.x;
        rcItemsRect.right  = rcItem.right;
        rcItemsRect.bottom = rcItem.bottom;

        if (GetExtendedStyle() & LVS_EX_CHECKBOXES)
            rcItemsRect.left -= GetSystemMetrics(SM_CXEDGE) + 16;
    }

    br.CreateSolidBrush(GetBkColor());

    if (rcItemsRect.IsRectEmpty())
        pDC->FillRect(rcCli, &br);
    else
    {
        if (rcItemsRect.left > rcCli.left)     // fill left rectangle
            pDC->FillRect(
                CRect(0, rcCli.top, rcItemsRect.left, rcCli.bottom), &br);
        if (rcItemsRect.bottom < rcCli.bottom) // fill bottom rectangle
            pDC->FillRect(
                CRect(0, rcItemsRect.bottom, rcCli.right, rcCli.bottom), &br);
        if (rcItemsRect.right < rcCli.right) // fill right rectangle
            pDC->FillRect(
                CRect(rcItemsRect.right, rcCli.top, rcCli.right, rcCli.bottom), &br);
    }

    return TRUE;
}