function GetAssociatedIcon(const AExtension: string; ASmall: Boolean): HIcon;
var
Info: TSHFileInfo;
Flags: Cardinal;
begin
if ASmall then
Flags := SHGFI_ICON or SHGFI_LARGEICON or SHGFI_USEFILEATTRIBUTES;
SHGetFileInfo(PChar(AExtension), FILE_ATTRIBUTE_NORMAL, Info, SizeOf(TSHFileInfo), Flags);
Result := Info.hIcon;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
icon: TIcon;
ListItem: TListItem;
Line:Integer;
begin
ListView1.Items.Clear;
For Line := 0 To FileListBox1.Items.Count -1 do
begin
icon := TIcon.Create;
try
If (ExtractFileExt(FileListBox1.Items[Line]) = '.exe') or (ExtractFileExt(FileListBox1.Items[Line]) = '.EXE') Then
icon.Handle := ExtractIcon(
Handle, PChar(FileListBox1.Items[Line]), 0)
else
icon.Handle := GetAssociatedIcon(ExtractFileExt(FileListBox1.Items[Line]), True);
with ListView1 do
begin
ListItem := Items.Add;
Listitem.Caption := FileListBox1.Items[Line];
ListView1.LargeImages := ImageList1;
ListView1.ViewStyle := vsIcon;
ListItem.ImageIndex := ImageList1.AddIcon(Icon);
end;
finally
icon.Free;
end;
end;
end;