举个简单的例子:
function GetRootNode1(Node: TTreeNode): TTreeNode;
begin
if Node.Parent = nil then
Result := Node
else
Result := GetRootNode1(Node.Parent);
end;
function GetRootNode2(Node: TTreeNode): TTreeNode;
begin
Result := Node;
while Result.Parent <> nil do
Result := Result.Parent;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if TreeView1.Selected = nil then
Exit;
ShowMessage(GetRootNode1(TreeView1.Selected).Text);
ShowMessage(GetRootNode2(TreeView1.Selected).Text);
end;