IconHandle to ImageList

Posted on 2001-08-03
Medium Priority
Last Modified: 2008-02-01
I have got a handle to an Icon (hIcon) and want to
add the icon to an ImageList at Run-time...

I've tried the ImageList1.ListImages.Add( , ,hIcon) function but it doesn't accept the handle instead  
it wants the loadpicture statement or a picture,
how can I make the handle become a picture in some way...

I DO NOT want to DrawIconEx it to a picturebox and then extract the picture from there. There must be some better way to do it...
Question by:Marshow
  • 2
  • 2

Expert Comment

ID: 6348471
The only way I can see you doing it is to add the following:

Adding this to the top of the module code (outside any functions).

Private Declare Function ImageList_ReplaceIcon Lib "Comctl32.dll" (imagelist As Long, index As Integer, icon As Long) As Long

And placing this line where you want to add an icon the the imagelist.

ImageList_ReplaceIcon ilsImageList.hImageList, -1, hIcon

The -1 parameter basically appends the icon to the imagelist.
hIcon is the icon handle.


Author Comment

ID: 6349130
hmm, now I've tried it but it always return -1...
then i tried ImageList_AddIcon instead, this function
receives succesfully but the ImageList count doesn't seem to increase and the added image doesn't even exist in the imagelist.

Expert Comment

ID: 6349207
How have you loaded the icon?
Make sure the imagelist handle is not null...found that an empty imagelist returns a handle of null (pain).

the AddIcon function is basically a macro that calls the ReplaceIcon but takes care of the -1 for you.

Author Comment

ID: 6350340
Ok, here is my code
ImageList_ReplaceIcon still receives -1

Dim lRet As Long
Dim hIcon As Long
Dim sDisp As String
Dim sType As String

   sDisp = shInfo.szDisplayName
   sType = shInfo.szTypeName
   hIcon = shInfo.hIcon

   lRet = ImageList_ReplaceIcon(imlFile.hImageList, -1, shInfo.hIcon)
   'Useful code...
   DestroyIcon shInfo.hIcon
LVL 28

Accepted Solution

Ark earned 400 total points
ID: 6350996
Seems you're writing new explorer :). If so, more efficient is to bind system image list directly to your ListView/Treeview and not use VB ImageList control (OK, you need this control with ListView because of weired VB LV control behavior - if you not bind Image list control to it, icon for selected item disapered ???). Take a look on my code http://www.freevbcode.com/ShowCode.Asp?ID=2977.
You can find a lot of interesting and undocumented things there abot shell namesoace and common controls.

If you still want to use VB image list:

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 Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictDesc As PictDesc, riid As Guid, ByVal fPictureOwnsHandle As Long, ipic As IPicture) As Long

Private Function IconToPicture(ByVal hIcon As Long) As StdPicture
    If hIcon = 0 Then Exit Function
    Dim oNewPic As Picture
    Dim tPicConv As PictDesc
    Dim IGuid As Guid
    With tPicConv
       .cbSizeofStruct = Len(tPicConv)
       .PicType = vbPicTypeIcon
       .hImage = hIcon
    End With
    With IGuid
        .Data1 = &H7BF80980
        .Data2 = &HBF32
        .Data3 = &H101A
        .Data4(0) = &H8B
        .Data4(1) = &HBB
        .Data4(2) = &H0
        .Data4(3) = &HAA
        .Data4(4) = &H0
        .Data4(5) = &H30
        .Data4(6) = &HC
        .Data4(7) = &HAB
    End With
    OleCreatePictureIndirect tPicConv, IGuid, True, oNewPic
    Set IconToPicture = oNewPic
End Function

'In your case you don't need this, you use Icons, but for future using...

Private Function BitmapToPicture(ByVal hBmp As Long) As StdPicture
    Dim oNewPic As Picture, tPicConv As PictDesc, IGuid As Guid
    With tPicConv
       .cbSizeofStruct = Len(tPicConv)
       .PicType = vbPicTypeBitmap
       .hImage = hBmp
    End With
    With IGuid
       .Data1 = &H20400
       .Data4(0) = &HC0
       .Data4(7) = &H46
    End With
    OleCreatePictureIndirect tPicConv, IGuid, True, oNewPic
    Set BitmapToPicture = oNewPic
End Function

And now simply:
ImageList1.ListImages.Add( , ,IconToPicture(hIcon))


Featured Post

Hire Technology Freelancers with Gigs

Work with 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.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

627 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