GetFileIcon - Retrieve the icon associated to a file
Private Const MAX_PATH = 260
Private Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End Type
Private Declare Function SHGetFileInfo Lib "Shell32" Alias "SHGetFileInfoA" _
(ByVal pszPath As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, _
ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
Private Const SHGFI_ICON = &H100
Private Const SHGFI_OPENICON = &H2
Private Const SHGFI_SELECTED = &H10000
Private Const SHGFI_SHELLICONSIZE = &H4
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0
Public Enum mbIconSizeConstants
mbLargeIcon = SHGFI_LARGEICON '32x32 icon
mbSmallIcon = SHGFI_SMALLICON '16x16 icon
mbShellSizeIcon = SHGFI_SHELLICONSIZE 'size used by the shell to display
End Enum 'the icons (for example 32x32 or
' 48x48)
Public Enum mbIconTypeConstants
mbNormalIcon = SHGFI_ICON 'normal icon
mbSelectedIcon = SHGFI_SELECTED 'the icon when it is selected
mbOpenIcon = SHGFI_OPENICON 'the icon used for open folders
End Enum
' Returns the description of the specified file/folder (for example "Folder",
' "Executable file", "Bmp Image" and so on)
' Get the file/folder's associated icon
'
' NOTE: uses the IconToPicture function
' (you can find it elsewhere in the Code Bank)
Function GetFileIcon(ByVal sPath As String, Optional ByVal mbIconSize As _
mbIconSizeConstants = mbLargeIcon, Optional ByVal mbIconType As _
mbIconTypeConstants = mbNormalIcon) As StdPicture
Dim FInfo As SHFILEINFO
Dim lIconType As Long
lIconType = mbIconSize Or mbIconType
' be sure that there is the mbNormalIcon too
If mbIconType <> mbNormalIcon Then lIconType = lIconType Or mbNormalIcon
' retrieve the item's icon
SHGetFileInfo sPath, 0, FInfo, Len(FInfo), lIconType
' convert the handle to a StdPicture
Set GetFileIcon = IconToPicture(FInfo.hIcon)
End Function