Solved

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

Posted on 1998-05-28
9
349 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

808 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