Author: Tomas Rutkauskas
How can I get the icon of a registered extension (in the windows registry)?
Answer:
Solve 1:
First get the pointer to the operating systems image list and assign it to your own.
{Image List From System}
ImageListHandle := SHGetFileInfo('C:\', 0, FileInfo, SizeOf(FileInfo),
SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
SendMessage(ListView1.Handle, LVM_SETIMAGELIST, LVSIL_SMALL,
LParam(ImageListHandle));
Then in your listview you can extract out the icon by doing this:
1
2 function GetShellIcon(FileName: string; Folder: Boolean): Integer;
3 var
4 FileInfo: TSHFileInfo;
5 ImageHandle: THandle;
6 Flag: Integer;
7 begin
8 if Folder then
9 Flag := FILE_ATTRIBUTE_DIRECTORY
10 else
11 Flag := FILE_ATTRIBUTE_NORMAL;
12 SHGetFileInfo(PChar(FileName), Flag, FileInfo, SizeOf(FileInfo),
13 SHGFI_SYSICONINDEX or SHGFI_SMALLICON or SHGFI_UseFileAttributes);
14 Result := FileInfo.IIcon;
15 end;
Notice: At the bottom I assign the icon index from the IICON field of the data
returned by SHGetFileInfo. So my function gets passed a filename and returns an
image index. You could change the IICON field to the HICON field and then I believe
you would be getting the handle to an icon.
Solve 2:
The SHGFI_SYSICONINDEX in the above code snippet is going to place the index of the
icon in the system image list (a handle to which is being returned as the result of
the SHGetFileInfo function call) in the FileInfo.iIcon member. Use the SHGFI_ICON
flag instead to get an icon handle in HIcon.
16
17 function GetShellIcon(FileName: string; Folder: Boolean): Integer;
18 var
19 FileInfo: TSHFileInfo;
20 ImageHandle: THandle;
21 Flag: Integer;
22 IconHandle: THandle;
23 begin
24 if Folder then
25 Flag := FILE_ATTRIBUTE_DIRECTORY
26 else
27 Flag := FILE_ATTRIBUTE_NORMAL;
28 SHGetFileInfo(PChar(FileName), Flag, FileInfo, SizeOf(FileInfo),
29 SHGFI_ICON or SHGFI_SMALLICON or SHGFI_UseFileAttributes);
30 Result := FileInfo.IIcon;
31 IconHandle := FileInfo.HIcon;
32 end;
Solve 3:
One method of getting the associated icon with a particular file is to first
populate an imagelist with registered system icons (this example is for small
icons, you could do the same for large). In this example, iIndex will be the
ImageList item for the particular file.
33 var
34 dwSmallIcon: DWord;
35 pFileInfo: TshFileInfo;
36 iIndex: Integer;
37 begin
38 dwSmallIcon := SHGetFileInfo('', 0, FileInfo, SizeOf(TshFileInfo), (SHGFI_ICON or
39 SHGFI_SMALLICON or SHGFI_SYSICONINDEX));
40 ImageList1.Handle := dwSmallIcon; {imagelist}
41 ImageList1.ShareImages := True;
42 FillChar(pFileInfo, SizeOf(TshFileInfo), 0);
43 shGetFileInfo(pName, 0, pFileInfo, SizeOf(TshFileInfo), SHGFI_SYSICONINDEX or
44 SHGFI_SMALLICON or SHGFI_OPENICON);
45 iIndex := pFileInfo.iIcon;
46 end;
|