by Ray (1 Submission)
Category: Windows System Services
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 14th September 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)
This code extracts the icon from any file and saves it into a image list to be used in listviews, treeviews, and the like. It will extract the
API Declarations
Public Declare Function OleCreatePictureIndirect _
Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, _
ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Public Type PicBmp
Size As Long
tType As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type
Public Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Public Type SHFILEINFO
hicon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * 260
szTypeName As String * 80
End Type
' YOU MUST Make a Reference To Standard OLE Types
' Put a command button, listview, and imagelist on your form
' and set the imagelist properties of the listview to imagelist1
' and set the View property of the listview to lvwList
Public Function GetIconFromFile(FileName As String, IconIndex As Long, UseLargeIcon As Boolean) As Picture
Dim b As SHFILEINFO
Dim retval As Long
retval = SHGetFileInfo(FileName, 0, b, Len(b), &H100)
'IPicture requires a reference to "Standard OLE Types."
Dim pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
With pic
.Size = Len(b)
.tType = vbPicTypeIcon
.hBmp = b.hicon 'Handle to bitmap.
End With
'Create Picture object.
Call OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic)
'Return the new Picture object.
Set GetIconFromFile = IPic
End Function
Private Sub Command1_Click()
Dim i As Integer
Dim itm As ListItem
ImageList1.ListImages.Add , , GetIconFromFile("c:\anyValidFileorFolder", 0, True)
ListView1.Icons = ImageList1
For i = 1 To ImageList1.ListImages.Count
Set itm = ListView1.ListItems.Add(, , , , ImageList1.ListImages.Item(i).Index)
Next i
' Questions? Comments? Email to [email protected]
End Sub
No comments have been posted about This code extracts the icon from any file and saves it into a image list to be used in listviews, t. Why not be the first to post a comment about This code extracts the icon from any file and saves it into a image list to be used in listviews, t.