2
votes

I am trying to customize Background color and font color of a ListView item using OnCustomDrawItem event. However, the border color of subitem is always the background color of ListView. Does anyone know how to fix this? Here is the code i am using:

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
  lst: TListView;
  i: integer;
  f1, f2, c1, c2: TColor;
begin
  if (TListView(Sender).ViewStyle = vsIcon) then
    Exit;
  lst := Sender as TListView;
  lst.Canvas.Brush.Style := bsSolid;
  i := lst.Items.Count;
  if (i mod 2) <> 0 then
  begin
    c1 := clWhite;
    c2 := $00F8F8F8;
    f1 := clBlue;
    f2 := clBlack;
  end else
  begin
    c1 := $00F8F8F8;
    c2 := clWhite;
    f1 := clBlack;
    f2 := clBlue;
  end;
  // Painting...
  if (Item.Index mod 2) = 0 then
  begin
    lst.Canvas.Brush.Color := c2;
    lst.Canvas.Font.Color := f2;
  end else
  begin
    lst.Canvas.Brush.Color := c1;
    lst.Canvas.Font.Color := f1;
  end;
end;

EDIT:

There is a GAP between column of SubItems. This GAP is the color of ListView's background.

I use Delphi XE2 and OS: Windows 7 bit bit.

1
Hint: Replace (Item.Index mod 2) = 0 by Odd(Item.Index) (or the negation of the last statement). - Andreas Rejbrand
Btw, there is a gap between columns of subitems... and this gap is the color of listview - blacksun
Btw, I use XE2 and Win7 64bit - blacksun
Are you sure it is not themed? By default apps are themed. Perhaps you thought I was asking if you used VCL styles. If your app is not themed then you would have had to remove the comctl32 v6 manifest. Did you do that? - David Heffernan
Oppsss.. My mistake then. Yes it is themed. How can i remove this theme from code? - blacksun

1 Answers

1
votes

Try OnAdvancedCustomDrawItem instead your CustomDrawItem procedure

procedure TForm1.ListView1AdvancedCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
var DefaultDraw: Boolean);
var
  lst: TListView;
  i: integer;
  f1, f2, c1, c2: TColor;
  r: TRect;
begin
  if Stage = cdPostPaint then
  begin
    lst := Sender as TListView;
    lst.Canvas.Brush.Style := bsSolid;
    i := lst.Items.Count;
    if not Odd(i) then
    begin
      c1 := clWhite;
      c2 := $00F8F8F8;
      f1 := clBlue;
      f2 := clBlack;
    end else
    begin
      c1 := $00F8F8F8;
      c2 := clWhite;
      f1 := clBlack;
      f2 := clBlue;
    end;
    // Painting...
    if Odd(Item.Index) then
    begin
      lst.Canvas.Brush.Color := c2;
      lst.Canvas.Pen.Color := c2;
      lst.Canvas.Font.Color := f2;
    end else
    begin
      lst.Canvas.Brush.Color := c1;
      lst.Canvas.Pen.Color := c1;
      lst.Canvas.Font.Color := f1;
    end;

    r:=Item.DisplayRect(drBounds);

    if cdsSelected in State then
    begin
      lst.Canvas.Brush.Color:=clHighlight;
      lst.Canvas.Pen.Color:=clHighlight;
      lst.Canvas.Font.Color:=clBlack;
    end;

    lst.Canvas.Rectangle(r);

    if cdsSelected in State then
      lst.Canvas.DrawFocusRect(r);

    lst.Canvas.TextOut(r.Left, r.Top + (r.Bottom - r.top - lst.Canvas.TextHeight(Item.Caption)) div 2, Item.Caption);
    for i := 0 to Item.SubItems.Count - 1 do
    begin
      r.Left:=r.Left + lst.Columns[i].Width;
      lst.Canvas.TextOut(r.Left, r.Top + (r.Bottom - r.top - lst.Canvas.TextHeight(Item.Caption)) div 2, Item.SubItems[i]);
    end;
  end;
end;

Hope this helps.