我想更改虚拟字符串树的特定行中的文本颜色。可能吗?
我想更改虚拟字符串树的特定行中的文本颜色。可能吗?
使用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;
请注意,为TreeView中的每个单元调用此方法。节点指针在一行的每个单元格中是相同的。因此,如果您有多个列并希望根据特定列的内容设置整行的颜色,则可以使用给定的节点,如示例代码中所示。
要更改特定行中文本的颜色, 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;