Board index » cppbuilder » flicker in ListView

flicker in ListView


2006-03-06 11:34:48 AM
cppbuilder97
Hi,
I have a strange problem with flicker on my custom-drawn ListView. All
is fine, except when I change the font color from item to item, I get
flicker. Here's the code:
void __fastcall TGamesForm::ListView1AdvancedCustomDrawItem(
TCustomListView *Sender, TListItem *Item, TCustomDrawState State,
TCustomDrawStage Stage, bool &DefaultDraw)
{
DefaultDraw = false;
TListView *lv = (TListView *)(Sender);
FICSGame *g = (FICSGame *)(Item->Data);
TRect rect = Item->DisplayRect(drBounds);
if (State.Contains(cdsSelected))
{
TRect focus = rect;
lv->Canvas->Brush->Color = clYellow;
lv->Canvas->FrameRect(focus);
InflateRect(&focus, -1, -1);
lv->Canvas->Brush->Color = clNavy;
lv->Canvas->FillRect(focus);
lv->Font->Color = clWhite;
}
else
{
lv->Canvas->Brush->Color = clWhite;
lv->Canvas->FillRect(rect);
lv->Font->Color = clBlack;
}
if (g && g->Observed)
ImageList1->Draw(lv->Canvas, 1, rect.Top, 1, true);
if (g && g->Examined)
ImageList1->Draw(lv->Canvas, 19, rect.Top, 0, true);
TRect textrect(37, rect.top + 2, lv->Columns->Items[0]->Width - 4,
rect.bottom);
DrawText(lv->Canvas->Handle, Item->Caption.c_str(),
Item->Caption.Length(), &textrect, DT_END_ELLIPSIS);
textrect.Left = textrect.Right + 4;
textrect.Right += lv->Columns->Items[1]->Width;
DrawText(lv->Canvas->Handle, Item->SubItems->Strings[0].c_str(),
Item->SubItems->Strings[0].Length(), &textrect, DT_END_ELLIPSIS);
int last = Item->SubItems->Count;
if (lv != ListView1)
last--;
for (int count = 1; count < last; count++)
{
textrect.Left = textrect.Right + 4;
textrect.Right += lv->Columns->Items[count + 1]->Width;
DrawText(lv->Canvas->Handle,
Item->SubItems->Strings[count].c_str(),
Item->SubItems->Strings[count].Length(), &textrect, DT_END_ELLIPSIS);
}
}
OwnerDraw is set to false. The DoubleBuffered property of both the
ListView and the form is set to true. Also, for both, ControlStyle <<
csOpaque. The only time it flickers is when an item is selected and
the font color is changed from item to item (based on whether it's
selected). What could be the cause?
 
 

Re:flicker in ListView

For anyone who is interested:
I managed to figure out the problem. It seems like changing any part
of the Font property will force an update of the ListView. Of course
when this happens, OnAdvancedCustomDrawItem is called again, which
forces the Font property to be changed AGAIN, and on and on, which
causes the ListView to go into a perptual refresh mode. To get around
this, simply call the API function SetTextColor() instead of changing
any properties. I have learned the VCL can't do everything for you;
sometimes you must resort to good old-fashioned Win32 API!