不好意思,我没说清楚。
我是想定位到根节点,并且根节点的颜色成蓝色(就像选中了一样)。
var
treeNode: TTreeNode;
begin
TreeView1.HideSelection:=false;
treeNode:=TreeView1.Items.Item[0];
while treeNode.getFirstChild <> nil do
treeNode:=treeNode.getFirstChild;
treeNode.Selected:=true;
end;
node.Expand(true);
treeview1.selected.Expanded; //判断节点的子节点是否展开True展开,否则闭拢
treeview1.selected.Expand(True);//子节点展开
treeview1.selected.collapse(True)://子节点闭拢
树节点全部展开:
procedure TForm1.Button1Click(Sender: TObject);
var node:TTreeNode;
begin
if treeview1.Items[0]<>nil then
begin
node:=treeview1.Items[0];
node.Expand(true);
while node.getNextSibling<>nil do
begin
node:=node.getNextSibling;
node.Expand(true);
end;
end;
end;
树节点全部收缩:
procedure TForm1.Button2Click(Sender: TObject);
var node:TTreeNode;
begin
if treeview1.Items[0]<>nil then
begin
node:=treeview1.Items[0];
node.Collapse(true);
while node.getNextSibling<>nil do
begin
node:=node.getNextSibling;
node.Collapse(true);
end;
end;
end;
节点高亮显示
如果设置了TreeView.HideSelection:=False;
if node.Selected then
begin
Canvas.Brush.Style := bsFDiagonal;
Canvas.Brush.Color := clHighlight;
Canvas.Font.Color := clHighlightText;
end;
OnCustomDrawItem事件里只需一句TreeView1.Canvas.Brush.Style := bsFDiagonal;就可以了
TreeView1.Select(node);