uses
ShellAPI;
var
IconList:TStringList;
procedure ListView_SetItemImageIndex(Item: TListItem);
var
nIndex:Integer;
Icon:TIcon;
fileName:string;
extName:string;
sinfo:SHFILEINFO;
begin
if TListView(Item.ListView).SmallImages<>nil then
begin
fileName:=Item.Caption;
extName:=ExtractFileExt(fileName);
nIndex:=IconList.IndexOf(extName);
if nIndex>-1 then
begin
nIndex:=Integer(IconList.Objects[nIndex]);
Item.ImageIndex:=nIndex;
end else
begin
FillChar(sinfo, SizeOf(sinfo),0);
SHGetFileInfo(PChar(extName),FILE_ATTRIBUTE_NORMAL,sinfo,SizeOf(sInfo),SHGFI_USEFILEATTRIBUTES or SHGFI_ICON or SHGFI_SMALLICON);
if sinfo.hIcon>0 then
begin
Icon:=TIcon.Create;
Icon.Handle:=sinfo.hIcon;
nIndex:=TListView(Item.ListView).SmallImages.AddIcon(Icon);
Item.ImageIndex:=nIndex;
IconList.AddObject(extName,TObject(nIndex));
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Item:TListItem;
begin
Item:=ListView1.Items.Add;
Item.Caption:='c:/test.jpg';
ListView_SetItemImageIndex(Item);
end;
initialization
IconList:=TStringList.Create;
finalization
IconList.Free;
end.