如何更改 TTNTListView 中某一列的文本颜色?

发布于 2024-12-08 22:42:13 字数 662 浏览 2 评论 0原文

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

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。 如果我的代码有问题,请告诉我如何修复它。 如果这是一个错误,也许有人知道解决方法。 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪 2024-12-15 22:42:13

我猜您只需要为其他情况显式设置颜色即可。由于您没有这样做,画布状态仍然存在。试试这个:

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文