新建一个Application窗体上放置一个ImageList,Button,ListView,然后让ListView和ImageList相关联,然后写入下面的代码:
function TForm1.GetIconIndex(mPath: string): Integer;
var
vSHFileInfo: TSHFileInfo;
begin
FillChar(vSHFileInfo, SizeOf(vSHFileInfo), 0);
SHGetFileInfo(PChar(mPath), 0, vSHFileInfo, SizeOf(vSHFileInfo),
SHGFI_SYSICONINDEX);
Result := vSHFileInfo.iIcon;
end;
function TForm1.GetSystemImageList(mImageList: TImageList): Boolean;
var
mHandle: THandle;
vSHFileInfo: TSHFileInfo;
begin
FillChar(vSHFileInfo, SizeOf(vSHFileInfo), 0);
mHandle := SHGetFileInfo('', 0, vSHFileInfo, SizeOf(vSHFileInfo),
SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
Result := mHandle <> 0;
mImageList.Handle := mHandle;
mImageList.ShareImages := True;
end;
procedure TForm1.PathToListItem(mDirName: string);
var
vSearchRec: TSearchRec;
vPathName: string;
K: Integer;
vListItem: TListItem;
begin
vPathName := mDirName + '/*.*';
K := FindFirst(vPathName, faAnyFile, vSearchRec);
while K = 0 do
begin
if (vSearchRec.Attr and faDirectory <> 0) and (Pos(vSearchRec.Name, '..') = 0) then
begin
vListItem := ListView1.Items.Add;
vListItem.Caption := vSearchRec.Name;
vListItem.ImageIndex := GetIconIndex(mDirName + '/' + vSearchRec.Name);
PathToListItem(mDirName + '/' + vSearchRec.Name)
end
else if (Pos(vSearchRec.Name, '..') = 0) then
begin
vListItem := ListView1.Items.Add;
vListItem.Caption := vSearchRec.Name;
vListItem.ImageIndex := GetIconIndex(mDirName + '/' + vSearchRec.Name);
end;
K := FindNext(vSearchRec);
end;
FindClose(vSearchRec);
end;
///上面是自己定义的函数
procedure TForm1.Button1Click(Sender: TObject);
var
vDirectory: string;
begin
if not SelectDirectory('Path', '', vDirectory) then Exit;
PathToListItem(vDirectory);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
GetSystemImageList(ImageList1);
ImageList1.DrawingStyle := dsTransparent;
end;
编译运行,就可以了。