Author: Tomas Rutkauskas
How to fill a TListView with all files of a given directory along with the system
icons
Answer:
Here's some code from a recent project. FileList is a TListView. ScanDir() is a
function from our product - it's basically a procedure that fills a TStrings object
with a list of files matching a mask. You can ignore the TDirInfo(Node.Data) stuff
- it's a small class that holds info on each folder as displayed in a
TTreeView.TTreeNode.
This routine builds a TListView that's pretty much like the right pane in Windows
Explorer, in that it supports both the list and report view, and displays the file
type, size, and modified date in columns in report view.
1 {Gets files in a folder and displays them in the ListView}
2
3 procedure TExplorer.GetFilesInFolder(Node: TTreeNode);
4 var
5 SL: TStringList;
6 i: Integer;
7 Dat: TDirInfo;
8 AllSel: Boolean;
9 NewItem: TListItem;
10 FI: TSHFileInfo;
11 Dt: TDateTime;
12 TypeDesc: string;
13 begin
14 if not Assigned(Node) then
15 Exit;
16 SL := TStringList.Create;
17 if Screen.Cursor <> crHourglass then
18 Screen.Cursor := crHourglass;
19 try
20 {Need easier access to Node.Data than TDirInfo(Node.Data) typecasts}
21 {Grab a local reference to the pointer}
22 Dat := TDirInfo(Node.Data);
23 {Get files in this folder, but don't include subfolder files}
24 ScanDir(Dat.FullPath, '*.*', SL, False);
25 SL.Sorted := True;
26 {See if this folder has already been fully selected.
27 If so, we don't need to add it to the Folders list or increment selection count
28 or bytes}
29 AllSel := (Folders.IndexOf(Dat.FullPath) > -1) or (Dat.Status = dsFull);
30 {Remove stuff that was previously displayed}
31 FileList.Items.BeginUpdate;
32 FileList.Items.Clear;
33 {Is this an empty folder?}
34 if SL.Count = 0 then
35 begin
36 FileList.SmallImages := StateImages;
37 NewItem := FileList.Items.Add;
38 NewItem.Caption := ' No files ';
39 NewItem.ImageIndex := NoFilesIndex;
40 FileList.Enabled := False;
41 Exit;
42 end;
43 FileList.SmallImages := SysImages;
44 {We have files. Add each one to the ListView}
45 for i := 0 to SL.Count - 1 do
46 begin
47 {Create a new TListItem}
48 NewItem := FileList.Items.Add;
49 {Assign the filename portion}
50 NewItem.Caption := ExtractFileName(SL[i]);
51 FillChar(FI, SizeOf(TSHFileInfo), #0);
52 {Get the icon for display, as well as the file type, with one function call.
53 Note the flags:
54 SHGFI_SMALLICON - we want the small icon
55 SHGFI_SYSICONINDEX - we want the index into the system imagelist
56 SHGFI_TYPENAME - we want the file type description if there is one}
57 SHGetFileInfo(PChar(SL[i]), 0, FI, SizeOf(FI), SHGFI_ICON or SHGFI_SMALLICON
58 or SHGFI_SYSICONINDEX or SHGFI_TYPENAME);
59 {The subitems are only displayed in the 'detail' view, but they have
60 to be there all the time. See if Windows knows what type file this is}
61 TypeDesc := FI.szTypeName;
62 if TypeDesc = '' then
63 {Windows doesn't know - handle like Explorer does}
64 TypeDesc := Upper(ExtractFileExt(SL[i])) + ' file';
65 {Delete the period if we need to}
66 if Length(TypeDesc) > 1 then
67 begin
68 if TypeDesc[1] = '.' then
69 Delete(TypeDesc, 1, 1);
70 end;
71 {Display the file type description}
72 NewItem.SubItems.Add(TypeDesc);
73 {Here's the 'Size' column ...}
74 NewItem.SubItems.Add(Comma([GetFileSize(SL[i])], False));
75 {Assign the system imagelist index to this item}
76 NewItem.ImageIndex := FI.iIcon;
77 {Grab the file's time and date stamp and convert to Delphi TDateTime}
78 Dt := FileDateToDateTime(FileAge(SL[i]));
79 {Add the date column}
80 NewItem.SubItems.Add(DateToStr(Dt));
81 {Add the time column}
82 NewItem.SubItems.Add(FormatDateTime('hh:nn:ss ampm', Dt));
83 {If folder was fully selected, or this file was selected in a
84 previous visit to this folder, check it}
85 if AllSel or (Files.IndexOf(SL[i]) > -1) then
86 NewItem.Checked := True;
87 end;
88 FileList.Enabled := True;
89 finally
90 SL.Free;
91 FileList.Items.EndUpdate;
92 if Screen.Cursor <> crDefault then
93 Screen.Cursor := crDefault;
94 end;
95 end;
|