【问题标题】:Is it possible to change the color of a row in a virtual string tree?是否可以更改虚拟字符串树中行的颜色?
【发布时间】:2010-07-23 01:33:11
【问题描述】:

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

【问题讨论】:

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

标签: delphi virtualtreeview


【解决方案1】:

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

这将改变每隔一行的背景(如果行在同一级别上)。

【讨论】:

  • 如果我根本不想要颜色怎么办?比如去掉背景颜色我累了TargetCanvas.Brush.Style := bsClear;但是失败了
  • @MartinLoanel 您需要做更多的工作才能使整个控件透明。将其作为一个不同的问题提出,您可能会得到一些答案,或者有人可能已经这样做了。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 2012-04-11
      • 2020-11-07
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      相关资源
      最近更新 更多