unit Unit1;
{$mode objfpc}{$H+}
interface
uses
windows, ShellApi, Classes, SysUtils, FileUtil, Forms, Controls, Graphics,
Dialogs, ComCtrls, StdCtrls, LazUTF8;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
ImageList1: TImageList;
ListView1: TListView;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
spath: string;
SysStr: Widestring;
i: integer;
mIcon: TIcon;
SearchRec: TSearchRec;
ListItem: TListItem;
FileInfo: SHFILEINFOw;
begin
sPath := 'C:\Files'; // ejem: "f:" ó "sPath := 'C:\Documents and Settings\Propietario\Recent'";
if sPath[Length(sPath)]<>'\' then sPath := sPath+'\'; // agregar el slash si es ncesario
ListView1.SmallImages := ImageList1;
ListView1.ViewStyle := vsReport;
ListView1.Columns.Add;
ListView1.Columns.Add;
mIcon := TIcon.Create;
try
ListView1.Items.BeginUpdate;
i := FindFirst(sPath + '*.*', faAnyFile, SearchRec);
while i = 0 do
begin
application.ProcessMessages;
with ListView1 do
begin
//if ((SearchRec.Attr and FaDirectory <> FaDirectory) and (SearchRec.Attr and FaVolumeId <> FaVolumeID)) then // for only files
If (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
Begin
// ShowMessage(sPath+SR.Name); // mostrar el archivo encontrado ejem: f:\casas\hola.txt SR.Name=hola.txt
SysStr:=UTF16ToUTF8(sPath + SearchRec.Name);
ListItem := ListView1.Items.add;
SHGetFileInfo(PwideChar(SysStr), 0, FileInfo, SizeOf(FileInfo), SHGFI_DISPLAYNAME);
Listitem.Caption := UTF16ToUTF8(FileInfo.szDisplayName);
SHGetFileInfo(PwideChar(SysStr), 0, FileInfo, SizeOf(FileInfo), SHGFI_TYPENAME);
ListItem.SubItems.Add(UTF16ToUTF8(FileInfo.szTypeName));
SHGetFileInfo(PwideChar(SysStr), 0, FileInfo, SizeOf(FileInfo), SHGFI_ICON or SHGFI_SMALLICON);
mIcon.Handle := FileInfo.hIcon;
ListItem.ImageIndex := ImageList1.AddIcon(mIcon);
end;
end;
i := FindNext(SearchRec);
end;
finally
ListView1.items.EndUpdate;
mIcon.Free;
end;
end;
end.