delphilistviewitem

Set font color, font style and background color to ListView.Item.Caption


How to set font color, font style and background color to ListView.Item.Caption? My code, as you can see on image below, is not working.

Image

procedure TFMainForm.ListView1CustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
begin
  case SubItem of
    0:
      begin
        Sender.Canvas.Brush.Color := clLime;
        Sender.Canvas.Font.Color := clBlack;
        Sender.Canvas.Font.Style := [FsBOld];
      end;
    1:
      begin
        Sender.Canvas.Brush.Color := clLime;
        Sender.Canvas.Font.Color := clBlack;
        Sender.Canvas.Font.Style := [FsBOld];
      end;
    2:
      begin
        Sender.Canvas.Font.Color := clBlack;
        Sender.Canvas.Font.Style := [FsBOld];
      end;
    3:
      begin
        Sender.Canvas.Font.Color := clBlack;
        Sender.Canvas.Font.Style := [FsBOld];
      end;
    4:
      begin
        Sender.Canvas.Font.Color := clBlack;
        Sender.Canvas.Font.Style := [FsBOld];
      end;
  end;
end;

Solution

  • OnCustomDrawSubItem() draws only subitems. Use OnCustomDrawItem() to draw the items.

    procedure TForm24.ListView1CustomDrawItem(Sender: TCustomListView;
      Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
    begin
      Sender.Canvas.Brush.Color := clLime;
    end;
    
    procedure TForm24.ListView1CustomDrawSubItem(Sender: TCustomListView;
      Item: TListItem; SubItem: Integer; State: TCustomDrawState;
      var DefaultDraw: Boolean);
    begin
      Sender.Canvas.Brush.Color := clYellow;
    end;
    

    enter image description here