如何更改TTNTListView列中的文本颜色? [英] How change text color in a column in TTNTListView?

查看:96
本文介绍了如何更改TTNTListView列中的文本颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Delphi 7中使用了TTNTListView。
设置为vsReport。
在OnCustomDrawSubItem事件中,我使用以下代码:

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;

问题是所有> = 3的子项目都以与子项目2相同的颜色绘制。对于SubItem> = 3,Sender.Canvas.Font.Color为clBlack,但它们使用clRed和clGreen绘制。
如果我的代码有问题,请告诉我如何解决。
如果是bug,也许有人知道解决方法。
谢谢。

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;

这篇关于如何更改TTNTListView列中的文本颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆