Einzelnen Beitrag anzeigen

marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#2

Re: Pfad von Treeview bekommen

  Alt 10. Okt 2007, 09:58
Moin,

ich habe ein paar Sachen umgestellt und etwas hinzugefügt. Schau mal wie weit du das ohne Kommentare durchdringen kannst.

Delphi-Quellcode:
procedure GetDirectories(
  Tree: TTreeView;
  Directory: string;
  Item: TTreeNode;
  IncludeFiles: Boolean
);
var
  SearchRec: TSearchRec;
  ItemTemp: TTreeNode;
begin
  Directory := IncludeTrailingPathDelimiter(Directory);
  if FindFirst(Directory + '*.*', faDirectory or faReadOnly or faArchive, SearchRec) = 0 then
  begin
    repeat
      if Pos(SearchRec.Name, '..') = 1 then
        Continue else
      if (SearchRec.Attr and faDirectory = faDirectory) then
        GetDirectories(
          Tree,
          Directory + SearchRec.Name,
          Tree.Items.AddChildObject(Item, SearchRec.Name, Pointer(1)),
          IncludeFiles
        ) else
      if IncludeFiles then
        Tree.Items.AddChild(Item, SearchRec.Name);
    until FindNext(SearchRec) <> 0;
    FindClose(SearchRec);
  end;
end;

function TreeNodePath(node: TTreeNode; delimiter: Char = PathDelim): string;
begin
  if Assigned(node)
    then Result := TreeNodePath(node.Parent, delimiter) + delimiter + node.Text
    else Result := '';
end;

procedure TDemoForm.ButtonClick(Sender: TObject);
begin
  TreeView.Items.BeginUpdate;
  GetDirectories(TreeView, 'C:\Daten', nil, False);
  TreeView.Items.EndUpdate;
end;

procedure TDemoForm.TreeViewChange(Sender: TObject; Node: TTreeNode);
begin
  Caption := TreeNodePath(Node);
end;
Grüße vom marabu
  Mit Zitat antworten Zitat