• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

How to extract icons directly from a file into a ListView?

I have a ListView control that I use icons in. And I use an Image control, that I link to the ListView. That workes fine, but I want to extract the icons directly from files and into the ListView. How do I do this?
  • 3
  • 2
  • 2
  • +2
1 Solution

Here is a sample of code that I got from Bin Huwairib to extract the file's icon.

1- Add form1 to project1.
2- Add listview1 to form1.
3- Add imagelist1 to form1.
4- Paste the following:

Private Declare Function SHGetFileInfo Lib "shell32" Alias "SHGetFileInfoA" _
                              (ByVal pszPath As Long, _
                              ByVal dwFileAttributes As Long, _
                              psfi As SHFILEINFO, _
                              ByVal cbFileInfo As Long, _
                              ByVal uFlags As Long) As Long

Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictDesc As PictDesc, riid As Guid, ByVal fPictureOwnsHandle As Long, ipic As IPicture) As Long

Private Type PictDesc
 cbSizeofStruct As Long
 picType As Long
 hImage As Long
 xExt As Long
 yExt As Long
End Type

Private Type Guid
 Data1 As Long
 Data2 As Integer
 Data3 As Integer
 Data4(0 To 7) As Byte
End Type

 hIcon As Long
 iIcon As Long
 dwAttributes As Long
 szDisplayName As String * 520
 szTypeName As String * 80
End Type

Const SHGFI_ICON = &H100

Private Sub Command1_Click()
 AddFile "d:\winnt\clock.avi", ListView1
End Sub

Private Sub AddFile(FilePath As String, Lvw As ListView)
 Call SHGetFileInfo(StrPtrA(CStr(FilePath)), 0, sfi, Len(sfi), SHGFI_ICON Or SHGFI_SMALLICON)
 ImageList1.ListImages.Add , , ConvertIcon(sfi.hIcon)
 Lvw.Icons = ImageList1
 Lvw.SmallIcons = ImageList1

 Lvw.ListItems.Add , , FilePath, 1
End Sub

Private Function StrPtrA(sz As String) As Long
  Dim asz() As Byte
  asz() = sz & vbNullChar
  asz() = StrConv(asz(), vbFromUnicode)
  StrPtrA = StrPtr(asz())
End Function

Function ConvertIcon(hIcon) As Picture
 Dim NewPic As Picture
 Dim PicConv As PictDesc
 Dim IGuid As Guid

 PicConv.cbSizeofStruct = Len(PicConv)
 PicConv.picType = vbPicTypeIcon
 PicConv.hImage = hIcon

 IGuid.Data1 = &H20400
 IGuid.Data4(0) = &HC0
 IGuid.Data4(7) = &H46

 Call OleCreatePictureIndirect(PicConv, IGuid, True, NewPic)

 Set ConvertIcon = NewPic
End Function

If you're implying that you do not want an imagelist control on your form at all them you might have to use the ImageList API to draw the icon onto the ListView control through ImageList_Draw.  However, I am not exactly sure how this API works in relation to a ListView control.  Still, it is the API upon which the ImageList control is based and I assume would greater functionality.  If I can find any more info on it, I post another comment.
As in extracting certain icons from dll or exe files and displaying them, or displaying the icon associated with a particular file?
I think the only way is to have a imagelist, if you alredy have one, create another only to load from file.

Load icon to the imagelist, view it in the image control, and if you like it, copy it to the old imagelist.

The new imagelist will be a kind of clipboard to the icons.


Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Olli083097Author Commented:
Cardo: Displaying the icon associated with a particular file in the ListView! When I use ListView1.AddItem "MyFile.zip", I want the icon associated with zip files to be the icon shown!

hozempa: I'm going to try you code and see what I can do with it!
Are you willing to have an Image list control still?  I know a method you could do this, but you may have to pass the icon through a form, or picture control or something, would this be acceptable?
   I think u should use imagelist and put the desired images inside. Then, to create icons form the files inserted in imagelist, put a maskcolor(Gray is default) in imagelist color property(Go into custom of imagelist). This color will make your file transparent as .ico file, when u use the following code:

 Dim picX As Picture
    Set picX = ImageList1.ListImages(1).ExtractIcon
    With Form1
        .MouseIcon = picX   ' Set new icon.
        .MousePointer = vbCustom    ' Set to custom icon.
    End With

Try this out. Best luck -
Olli083097Author Commented:
Thanx for trying to help me, but hozempa's answer was just what I wanted...

I have reopened the question, so if you want some points for youre help. Then please answer the question, and I'll grade it for you...
I'm glad to hear that the code is what you wanted and thank you for the points.

Olli083097Author Commented:
Thanx, but for others: You need a CommandButton added to the form.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now