【发布时间】:2010-07-23 01:33: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;
这将改变每隔一行的背景(如果行在同一级别上)。
【讨论】:
TargetCanvas.Brush.Style := bsClear;但是失败了
要控制特定行中文本的颜色,请使用 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。
【讨论】:
要更改特定行中文本的颜色,可以使用 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;
【讨论】: