These files demonstrate how to read and cache icon indizes for a TListView in the background.
- Some kind of cleanup could be helpful for long term use.
To use this in Delphi, unit fgl would need to be replaced with Generics.Collections (I simply prefer fgl).
Set the ListView field to use standard handling of your list items. This works best with OnCreateItemClass using a descendant of TFileIconListItem.
This is also uses the imagelist assigned to the ListView, if none has been specified yet.
To use an image list independent of a TListView, you can use this property and implement the OnGetNext and OnReceiveIconIndex events.
If you use a custom TListItem class based on TFileIconListItem, you do not need to specify the OnGetNext event. If not, you need to use it to find the next unresolved list item, like for example:
var
liFirst: TListItem;
iFirst: integer;
begin
AFilename := '';
if lvFiles.Items.Count = 0 then begin
Exit;
end;
liFirst := lvFiles.TopItem;
if not Assigned(liFirst) then begin
liFirst := lvFiles.Items[0];
end;
iFirst := liFirst.Index;
while (iFirst < lvFiles.Items.Count) do begin
if lvFiles.Items[iFirst].ImageIndex = IconCacheNoIconLoaded then begin
AFilename := FPath + lvFiles.Items[iFirst].Caption;
lvFiles.Items[iFirst].ImageIndex := IconCacheIconPending;
Exit;
end;
Inc(iFirst);
end;
end;
If you use a custom TListItem class based on TFileIconListItem, you do not need to specify the OnReceiveIconIndex index. If not, you need to use it to find the list item to assign the image index, like for example:
var
li: TListItem;
begin
li := lvFiles.Items.FindCaption(0, ExtractFileName(AFilename), False, True, False);
if not Assigned(li) then begin
Exit;
end;
li.ImageIndex := AIconIndex;
end;