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

0sgqnhkj  于 2022-11-04  发布在  其他
关注(0)|答案(2)|浏览(703)

如何设置字体颜色,字体样式和背景颜色为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;
vlju58qv

vlju58qv1#

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;

0yycz8jy

0yycz8jy2#

下面是为单个Subitem单元格着色的方法:(在我的例子中,子项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

相关问题