是否可以更改虚拟字符串树中行的颜色?

发布于 2024-09-11 09:30:47 字数 32 浏览 3 评论 0原文

我想更改虚拟字符串树特定行中文本的颜色。是否可以?

I want to change the color of text in a specific row of a virtual string tree. is it possible?

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

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

发布评论

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

评论(3

趁年轻赶紧闹 2024-09-18 09:31:14

要更改特定行中文本的颜色,可以使用 OnDrawText 事件来更改当前的 TargetCanvas.Font.Color 属性。

下面的代码适用于 Delphi XE 1 和虚拟树视图 5.5.2 ( http://virtual -treeview.googlecode.com/svn/branches/V5_stable/

type
  TFileVirtualNode = packed record
    filePath: String;
    exists: Boolean;
  end;

  PTFileVirtualNode  = ^TFileVirtualNode ;

procedure TForm.TVirtualStringTree_OnDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas; Node: PVirtualNode;
  Column: TColumnIndex; const Text: UnicodeString; const CellRect: TRect; var DefaultDraw: Boolean);
var
  pileVirtualNode: PTFileVirtualNode;
begin
  pileVirtualNode:= Sender.GetNodeData(Node);

  if not pileVirtualNode^.exists then 
  begin
    TargetCanvas.Font.Color := clGrayText;
  end;
end;

To change the color of text in a specific row, OnDrawText event can be used in which you change current TargetCanvas.Font.Color property.

The code below works with Delphi XE 1 and virtual treeview 5.5.2 ( http://virtual-treeview.googlecode.com/svn/branches/V5_stable/ )

type
  TFileVirtualNode = packed record
    filePath: String;
    exists: Boolean;
  end;

  PTFileVirtualNode  = ^TFileVirtualNode ;

procedure TForm.TVirtualStringTree_OnDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas; Node: PVirtualNode;
  Column: TColumnIndex; const Text: UnicodeString; const CellRect: TRect; var DefaultDraw: Boolean);
var
  pileVirtualNode: PTFileVirtualNode;
begin
  pileVirtualNode:= Sender.GetNodeData(Node);

  if not pileVirtualNode^.exists then 
  begin
    TargetCanvas.Font.Color := clGrayText;
  end;
end;
春夜浅 2024-09-18 09:31:08

要控制特定行中文本的颜色,请使用 OnPaintText 事件并设置 TargetCanvas.Font.Color。

procedure TForm.TreePaintText(Sender: TBaseVirtualTree; const TargetCanvas: 
  TCanvas; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType);
var
  YourRecord: PYourRecord;

begin
  YourRecord := Sender.GetNodeData(Node);

  // an example for checking the content of a specific record field
  if YourRecord.Text = 'SampleText' then 
    TargetCanvas.Font.Color := clRed;
end;

请注意,TreeView 中的每个单元格都会调用此方法。行的每个单元格中的节点指针都是相同的。因此,如果您有多个列,并且想要根据特定列的内容设置整行的颜色,您可以像示例代码中那样使用给定的 Node。

To control the color of the text in a specific row, use the OnPaintText event and set TargetCanvas.Font.Color.

procedure TForm.TreePaintText(Sender: TBaseVirtualTree; const TargetCanvas: 
  TCanvas; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType);
var
  YourRecord: PYourRecord;

begin
  YourRecord := Sender.GetNodeData(Node);

  // an example for checking the content of a specific record field
  if YourRecord.Text = 'SampleText' then 
    TargetCanvas.Font.Color := clRed;
end;

Note that this method is called for every cell in the TreeView. The Node pointer is the same in each cell of a row. So if you have multiple columns and want to set the color for a whole row accoring to the content of a specific column, you can use the given Node like in the example code.

埖埖迣鎅 2024-09-18 09:30:58

使用 OnBeforeCellPaint 事件:

procedure TForm1.VirtualStringTree1BeforeCellPaint(Sender: TBaseVirtualTree;
  TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
  CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
begin
  if Node.Index mod 2 = 0 then
  begin
    TargetCanvas.Brush.Color := clFuchsia;
    TargetCanvas.FillRect(CellRect);
  end;
end;

这将更改每隔一行的背景(如果行位于同一级别)。

Use the OnBeforeCellPaint event:

procedure TForm1.VirtualStringTree1BeforeCellPaint(Sender: TBaseVirtualTree;
  TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
  CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
begin
  if Node.Index mod 2 = 0 then
  begin
    TargetCanvas.Brush.Color := clFuchsia;
    TargetCanvas.FillRect(CellRect);
  end;
end;

This will change the background on every other row (if the rows are on the same level).

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