在Delphi中使用树形控件TreeView,改变其颜色,使用的是CustomDrawItem方法:
代码
procedure TForm1.tv1CustomDrawItem(Sender: TCustomTreeView;
Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if Odd(Node.AbsoluteIndex) then
begin
Sender.Canvas.Brush.Color:=clGreen; //改变背景颜色
Sender.Canvas.Font.Color:=clWhite; //改变字体颜色
end
else
Sender.Canvas.Font.Color:=clRed;
end;
Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if Odd(Node.AbsoluteIndex) then
begin
Sender.Canvas.Brush.Color:=clGreen; //改变背景颜色
Sender.Canvas.Font.Color:=clWhite; //改变字体颜色
end
else
Sender.Canvas.Font.Color:=clRed;
end;
在根据内容条件改变时,使用Node判断:
代码
procedure TForm1.tv1CustomDrawItem(Sender: TCustomTreeView;
Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if Length(Node.Text)<5 then
tv1.Canvas.Brush.Color:=clRed
else
tv1.Canvas.Brush.Color:=clWhite;
end;
Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if Length(Node.Text)<5 then
tv1.Canvas.Brush.Color:=clRed
else
tv1.Canvas.Brush.Color:=clWhite;
end;