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


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


1349
2017-07-23 01:33


起源

那么,你的问题得到了解答吗? - Nat


答案:


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

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


9
2017-07-23 02:24



如果我根本不想要颜色怎么办?喜欢删除我厌倦的背景颜色 TargetCanvas.Brush.Style := bsClear; 但失败了 - MartinLoanel
@MartinLoanel您需要做更多工作才能使整个控件透明化。问这是一个不同的问题,你可能会得到一些答案或有人可能已经做过。 - Nat
已经找到了办法 - MartinLoanel


若要控制特定行中文本的颜色,请使用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中的每个单元调用此方法。节点指针在一行的每个单元格中是相同的。因此,如果您有多个列并希望根据特定列的内容设置整行的颜色,则可以使用给定的节点,如示例代码中所示。


7
2017-07-11 10:18





要更改特定行中文本的颜色, 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;

0
2017-12-30 14:40