Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • 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?
0
Olli083097
Asked:
Olli083097
  • 3
  • 2
  • 2
  • +2
1 Solution
 
hozempaCommented:
Olli,

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

Example
=======
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

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

Const SHGFI_SMALLICON = &H1
Const SHGFI_SYSICONINDEX = &H4000
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)
 Dim sfi As SHFILEINFO
 
 Call SHGetFileInfo(StrPtrA(CStr(FilePath)), 0, sfi, Len(sfi), SHGFI_ICON Or SHGFI_SMALLICON)
 ImageList1.ListImages.Clear
 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.
0
 
CardoCommented:
As in extracting certain icons from dll or exe files and displaying them, or displaying the icon associated with a particular file?
0
 
fguerreiro_inixCommented:
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.

Regards

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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!
Thanx!
0
 
CardoCommented:
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?
0
 
minaxiCommented:
Olli,
   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 -
Regards
Meenakshi
0
 
Olli083097Author Commented:
minaxi:
Thanx for trying to help me, but hozempa's answer was just what I wanted...

hozempa:
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...
Thanx!!!
0
 
hozempaCommented:
I'm glad to hear that the code is what you wanted and thank you for the points.

hozempa
0
 
Olli083097Author Commented:
Thanx, but for others: You need a CommandButton added to the form.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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