Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1998-05-28
9
Medium Priority
?
363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…
Suggested Courses

610 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