开发者

How change text color in a column in TTNTListView?

开发者 https://www.devze.com 2023-04-11 09:51 出处:网络
I use a TTNTListView in Delphi 7. It is set to vsReport. At OnCustomDrawSubItem event I use this code: procedure TForm.ListViewCustomDrawSubItem(Sender: TCustomListView;

I use a TTNTListView in Delphi 7. It is set to vsReport. At OnCustomDrawSubItem event I use this code:

procedure TForm.ListViewCustomDrawSubItem(Sender: TCustomListView;
   Item开发者_如何学Go: TListItem; SubItem: Integer; State: TCustomDrawState;
   var DefaultDraw: Boolean);
begin
   if SubItem = 2 then
      if (Item.SubItems.Strings[1] = Text1) or (Item.SubItems.Strings[1] = Text2) then
         Sender.Canvas.Font.Color := clGreen
      else
         Sender.Canvas.Font.Color := clRed;
end;

The problem is that all subitems >= 3 are drawn with the same color as subitem 2. I checked and for SubItem >= 3 Sender.Canvas.Font.Color is clBlack but they are drawn with clRed and clGreen. If it's a problem in my code please show me how to fix it. If it's a bug maybe someone knows a workaround. Thank you.


I'd guess that you simply need to explicitly set the color for the other cases. Since you aren't doing so the canvas state persists. Try this:

procedure TForm.ListViewCustomDrawSubItem(Sender: TCustomListView;
   Item: TListItem; SubItem: Integer; State: TCustomDrawState;
   var DefaultDraw: Boolean);
var
  Color: TColor;
begin
  if SubItem = 2 then
    if (Item.SubItems.Strings[1] = Text1) or (Item.SubItems.Strings[1] = Text2) then
      Color := clGreen
    else
      Color := clRed;
  else
    Color := clBlack;
  Sender.Canvas.Font.Color := Color;
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号