uses shellapi, comctrls;
var
lv: TListview;
il: TImagelist;
procedure TForm1.FormCreate(Sender: TObject);
begin
// TListview und Timagelist werden hier dynamisch erzeugt.
// Man kann sie natürlich auch einfach auf die Form setzen und
// die Einstellungen im Objektinspektor vornehmen.
lv := TListview.create(self);
lv.parent := self;
il := TImagelist.create(self);
lv.smallimages := il;
lv.setbounds(10, 10, 470, 200);
lv.viewstyle := vsreport;
lv.Columns.Add.Width := 180;
lv.Columns.Add.Width := 70;
lv.Columns.Add.Width := 200;
lv.Columns[0].Caption := 'Name';
lv.Columns[1].Caption := 'Größe';
lv.Columns[2].Caption := 'Typ';
lv.Columns[1].alignment := taRightjustify;
// --------------------------------------------------------------
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// -------------
lv.free;
il.free;
// -------------
end;
function CustomSortProc
(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
begin
Result := lstrcmp(PChar(Item1.Caption), PChar(Item2.Caption));
end;
procedure einlesen(pfad: string);
var
sr: TWin32FindData;
h: THandle;
listitem: TListitem;
FileInfo: SHFileInfo;
gr, x: integer;
Icon: TIcon;
hlp: TListview;
s: string;
begin
screen.cursor := crHourglass;
il.clear;
lv.items.clear;
hlp := TLIstview.create(nil);
hlp.visible := false;
hlp.parent := lv;
hlp.sorttype := sttext;
Icon := TIcon.Create;
gr := sizeof(FileInfo);
if ansilastchar(pfad) <> '\' then pfad := pfad + '\';
lv.items.beginupdate;
h := FindFirstFile(PChar(pfad + '*.*'), sr);
if h <> INVALID_HANDLE_VALUE then repeat
if sr.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY = 0 then begin
listitem := hlp.items.add;
if string(sr.cfilename) = '?' then
listitem.caption := sr.cAlternateFileName
else listitem.caption := sr.cFilename;
listitem.subitems.add(inttostr(round(
(sr.nFileSizeHigh * MAXDWORD + sr.nFileSizeLow) / 1024 + 0.49)) + ' KB');
SHGetFileInfo(pchar(pfad + listitem.caption), 0,
FileInfo, gr, SHGFI_TypeName or SHGFI_Icon);
if trim(FileInfo.szTypename) = '' then begin
s := ansiuppercase(extractfileext(sr.cFilename));
if s <> '' then s := s + '-';
listitem.subitems.add(uppercase(copy(s, 2, length(s) - 1)) + 'Datei');
end else
listitem.subitems.add(FileInfo.szTypename);
Icon.handle := FileInfo.hIcon;
listitem.Imageindex := il.addIcon(ICon);
end else if
(string(sr.cFileName) <> '.') and (string(sr.cFileName) <> '..') then begin
listitem := lv.items.add;
listitem.caption := sr.cFilename;
listitem.subitems.add('');
SHGetFileInfo(pchar(pfad + sr.cFileName), 0,
FileInfo, gr, SHGFI_TypeName or SHGFI_ICON);
listitem.subitems.add(FileInfo.szTypename);
Icon.handle := FileInfo.hIcon;
listitem.Imageindex := il.addIcon(Icon);
end;
until Findnextfile(h, sr) = false;
lv.CustomSort(@CustomSortProc, 0);
for x := 0 to hlp.items.count - 1 do begin
listitem := lv.items.add;
listitem.caption := hlp.items[x].caption;
listitem.imageindex := hlp.items[x].imageindex;
listitem.subitems.add(hlp.items[x].subitems[0]);
listitem.subitems.add(hlp.items[x].subitems[1]);
end;
Icon.free;
hlp.free;
lv.items.endupdate;
screen.cursor := crDefault;
end;
// Beispielaufruf
procedure TForm1.Button2Click(Sender: TObject);
begin
einlesen('c:\windows');
end;