• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 562
  • Last Modified:

IconHandle to ImageList

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...
  • 2
  • 2
1 Solution
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.

MarshowAuthor Commented:
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.
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.
MarshowAuthor Commented:
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
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))

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

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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