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

Delphi 是否可以更改虚拟字符串树中行的颜色?,delphi,virtualtreeview,Delphi,Virtualtreeview,我想更改虚拟字符串树中特定行中文本的颜色。是否可能?使用OnBeforeCellPaint事件: procedure TForm1.VirtualStringTree1BeforeCellPaint(Sender: TBaseVirtualTree; TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; CellPaintMode: TVTCellPaintMode; CellRect: TRect; var C

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

使用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;

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

要控制特定行中文本的颜色,请使用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;

请注意,此方法是为树视图中的每个单元格调用的。行的每个单元格中的节点指针相同。因此,如果您有多个列,并且希望根据特定列的内容设置整行的颜色,您可以使用示例代码中的给定节点。

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

下面的代码与Delphi XE 1和virtual treeview 5.5.2()


那么,你的问题得到回答了吗?如果我根本不想要颜色怎么办?如删除背景色i tire
TargetCanvas.Brush.Style:=bsClear但是fail@MartinLoanel要使整个控件透明,您需要做更多的工作。把它作为一个不同的问题来问,你可能会得到一些答案,或者有人已经做了。
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;