Solved

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

Posted on 1998-05-28
9
345 Views
Last Modified: 2008-02-01
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
Comment
Question by:Olli083097
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 1

Expert Comment

by:hozempa
ID: 1461944
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
 
LVL 1

Expert Comment

by:Cardo
ID: 1461945
As in extracting certain icons from dll or exe files and displaying them, or displaying the icon associated with a particular file?
0
 
LVL 3

Expert Comment

by:fguerreiro_inix
ID: 1461946
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
 

Author Comment

by:Olli083097
ID: 1461947
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Expert Comment

by:Cardo
ID: 1461948
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
 

Expert Comment

by:minaxi
ID: 1461949
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
 

Author Comment

by:Olli083097
ID: 1461950
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
 
LVL 1

Accepted Solution

by:
hozempa earned 100 total points
ID: 1461951
I'm glad to hear that the code is what you wanted and thank you for the points.

hozempa
0
 

Author Comment

by:Olli083097
ID: 1461952
Thanx, but for others: You need a CommandButton added to the form.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now