问题
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