How change text color in a column in TTNTListView?

China☆狼群 提交于 2019-12-07 19:03:21

问题


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: 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.


回答1:


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;


来源:https://stackoverflow.com/questions/7696499/how-change-text-color-in-a-column-in-ttntlistview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!