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

jho*_*zzz 13 delphi virtualtreeview

我想更改虚拟字符串树的特定行中的文本颜色.可能吗?

Nat*_*Nat 11

使用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;
Run Code Online (Sandbox Code Playgroud)

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


Eri*_*tel 7

要控制特定行中文本的颜色,请使用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;
Run Code Online (Sandbox Code Playgroud)

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