将字体颜色、字体样式和背景颜色设置为 ListView.Item.Caption

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

如何设置字体颜色、字体样式和背景颜色为ListView.Item.Caption? 您在下图中看到的我的代码无法正常工作。

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;

OnCustomDrawSubItem() 只绘制子项。使用OnCustomDrawItem()绘制物品。

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;

这就是为单个子项目单元格着色的方式: (在我的例子中,子项 Nr 4 的数据必须大于零,子项必须是 Nr 5。每次绘制单元格后,您必须再次将颜色设置为 clblack。

procedure TfrmCandlebot.ListView2CustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
begin
  if ((StrToInt(Item.SubItems[4]) >  0) and (Subitem=5)) then Sender.Canvas.Brush.Color:= clWebLimeGreen ;
  if ((StrToInt(Item.SubItems[4]) <  0) and (Subitem=5)) then Sender.Canvas.Brush.Color:= clWebOrangeRed;
  Sender.Canvas.Font.Color:= clblack;

end;

Picture with listview and colored cells