1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73:
| uses ShellApi;
procedure AddIcon(ANode: TTreeNode; AImageList: TImageList; const AFileName: string); var FileInfo: SHFILEINFO; Icon: TIcon; begin //Get The DisplayName SHGetFileInfo(PChar(AFileName), 0, FileInfo, SizeOf(FileInfo), SHGFI_DISPLAYNAME); ANode.Text := FileInfo.szDisplayName; //Get The Icon That Represents The File SHGetFileInfo(PChar(AFileName), 0, FileInfo, SizeOf(FileInfo), SHGFI_ICON or SHGFI_SMALLICON); // Create a temporary TIcon Icon := TIcon.Create; icon.Handle := FileInfo.hIcon; ANode.ImageIndex := AImageList.AddIcon(Icon); Icon.Free; // Destroy the Icon DestroyIcon(FileInfo.hIcon); end;
procedure GetDirectories(Tree: TTreeView; Directory: string; Item: TTreeNode; IncludeFiles: Boolean); var SearchRec: TSearchRec; ItemTemp: TTreeNode; begin if Directory[Length(Directory)] <> '\' then Directory := Directory + '\'; if FindFirst(Directory + '*.*', faDirectory, SearchRec) = 0 then begin repeat if (SearchRec.Attr and faDirectory = faDirectory) and (SearchRec.Name[1] <> '.') then begin if (SearchRec.Attr and faDirectory > 0) then begin Item := Tree.Items.AddChild(Item, SearchRec.Name); AddIcon(Item, Form1.ImageList1, Directory + SearchRec.Name); end; ItemTemp := Item.Parent; GetDirectories(Tree, Directory + SearchRec.Name, Item, IncludeFiles); Item := ItemTemp; end else if IncludeFiles then if SearchRec.Name[1] <> '.' then begin ItemTemp := Tree.Items.AddChild(Item, SearchRec.Name); AddIcon(ItemTemp, Form1.ImageList1, Directory + SearchRec.Name); end; until FindNext(SearchRec) <> 0; FindClose(SearchRec); end; end;
procedure TForm1.Button1Click(Sender: TObject); var Node: TTreeNode; Path: string; Dir: string; begin Dir := 'C:\WINDOWS\SYSTEM32\DRIVERS\'; Screen.Cursor := crHourGlass; TreeView1.Images := ImageList1; TreeView1.Items.BeginUpdate; try TreeView1.Items.Clear; GetDirectories(TreeView1, Dir, nil, True); finally Screen.Cursor := crDefault; TreeView1.Items.EndUpdate; end; end; |