Ever wanted to get the icon that a file uses? This simple Extract_Icon
function shows you how...
Private Const SHGFI_DISPLAYNAME = &H200
Private Const SHGFI_ICON = &H100
Private Const SHGFI_SYSICONINDEX =
&H4000
' get system icon index
Private Const SHGFI_LARGEICON =
&H0
' get large icon
Private Const SHGFI_SMALLICON =
&H1
' get small icon
Private Const ILD_TRANSPARENT = &H1
Public Const MAX_PATH = 260
Private Type SHFILEINFO 'Structure used by SHGetFileInfo
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End Type
Private Declare Function ImageList_Draw Lib "comctl32.dll" (ByVal
himl&, ByVal i&, ByVal hDCDest&, ByVal x&, ByVal y&, ByVal
flags&) As Long
Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias
"SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As
Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As
Long
Public Function ExtractIcon(picImage As PictureBox, sFile As String)
Dim himl As Long
Dim lpzxExeName As String
Dim lRet As Long
Dim sWinPath As String
Dim shinfo As SHFILEINFO
lpzxExeName = sFile
himl = SHGetFileInfo(lpzxExeName, 0&, shinfo, Len(shinfo),
SHGFI_SYSICONINDEX Or SHGFI_SMALLICON)
If himl <> 0 Then
picImage.AutoRedraw = True
lRet = ImageList_Draw(himl, shinfo.iIcon, picImage.hDC, 0, 0,
ILD_TRANSPARENT)
picImage.Refresh
ExtractIcon = True
End If
End Function