Strange behavior of customdrawn TListview

Giganews Newsgroups
Subject: Strange behavior of customdrawn TListview
Posted by:  wp_xxyyzz (werner.paml…@freenet.de)
Date: Sat, 10 Nov 2007

I am using the OnCustomDraw type of events to display color bars in a
TListView. But the listview behaves very strangely. This is my code to
draw a rectangle with black border and red cross fill pattern:

procedure DrawRect(ACanvas:TCanvas; ARect:TRect);
begin
  with ACanvas do begin
    // erase background
    Brush.Color := clWhite;
    Brush.Style := bsSolid;
    FillRect(ARect);
    InflateRect(ARect, -2, -2);
    // draw rectangle
    Pen.Color := clBlack;
    Pen.Style := psSolid;
    Brush.Color := clRed;
    Brush.Style := bsCross;
    Rectangle(ARect);
  end;
end;

This code is called from the OnCustomDraw event of the listview (in
report style, of course):

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
  R : TRect;
begin
  DefaultDraw := false;
  R := Item.DisplayRect(drBounds);
  DrawRect(Sender.Canvas, R);
end;

The problem is that the Listview always displays only a white rectangle
with black border, not the requested fill pattern.

When I remove, in "DrawRect", the FillRect call which erases the
background, the red fill pattern appears, but now the item is not
repainted correctly after resizing of the column.

I tested DrawRect with the OnPaint event handler of a Paintbox,
everything behaves as expected.

What am I doing wrong?

(Delphi 7 Pro)

Replies