function GetTreeNodePath(
const ANode: TTreeNode):
string;
var
I: integer;
begin
if Assigned(ANode)
then
if Assigned(ANode.Parent)
then
GetTreeNodePath:= GetTreeNodePath(ANode.Parent)+'
\'+ANode.Text
else
Result:= ANode.Text
else
Result:= '
';
if Result <> '
'
then Result:= Result+'
\';
I:= 1;
while I < Length(Result)
do
begin
if (Result[I] = '
\')
and (Result[I+1] = '
\')
then
begin
Delete (Result, I, 1);
Dec(I);
end;
Inc(I);
end;
end;
function FindSubDir(
const ADir:
String;
var ASubDir:
string): boolean;
var
S: TSearchRec;
R: integer;
begin
Result:= false;
ASubDir:= '
';
R:= FindFirst(IncludeTrailingPathDelimiter(ADir)+'
*.*', faDirectory, S);
while ((R = 0)
and (
not Result))
do
begin
if ((S.Attr
and faDirectory) = faDirectory)
then
if ((S.
Name <> '
.')
and (S.
Name <> '
..'))
then
begin
Result:= true;
ASubDir:= S.
Name;
end;
R:= FindNext(S);
end;
FindClose(S);
end;
procedure GetFirstAndSecondLevel(
const APath:
String;
const ATree: TTreeView;
const AStartNode: TTreeNode);
var
S: TSearchRec;
R: integer;
N: TTreeNode;
ASubPath:
string;
begin
R:= FindFirst(APath+'
*.*', faDirectory, S);
while R = 0
do
begin
if ((S.Attr
and faDirectory) = faDirectory)
then
if ((S.
Name <> '
.')
and (S.
Name <> '
..'))
then
begin
N:= ATree.Items.AddChild(AStartNode, S.
Name);
if FindSubDir(APath+S.
Name, ASubPath)
then ATree.Items.AddChild(N, ASubPath);
end;
R:= FindNext(S);
end;
FindClose(S);
end;
procedure IndicatePathTreeView(
const ATree: TTreeView; APath:
String);
begin
APath:= IncludeTrailingPathDelimiter(APath);
ATree.Items.Clear;
ATree.Items.BeginUpDate;
GetFirstAndSecondLevel(APath, ATree, ATree.Items.AddChild(
Nil, APath));
ATree.Items.EndUpDate;
ATree.Items.GetFirstNode.Expand(false);
end;
procedure IndicateSelectedNode(
const ATree: TTreeView);
var
ANode: TTreeNode;
begin
if ATree.Selected <> ATree.Items.GetFirstNode
then
begin
ATree.Items.BeginUpDate;
while ATree.Selected.getFirstChild <>
Nil do
ATree.Items.Delete(ATree.Selected.getFirstChild);
ANode:= ATree.Selected;
GetFirstAndSecondLevel(GetTreeNodePath(ANode), ATree, ANode);
ATree.Items.EndUpDate;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
IndicatePathTreeView(TreeView1, '
C:');
end;
procedure TForm1.TreeView1Click(Sender: TObject);
begin
IndicateSelectedNode(TreeView1);
end;
procedure TForm1.TreeView1KeyUp(Sender: TObject;
var Key: Word; Shift: TShiftState);
begin
TreeView1Click(Sender);
end;