IconHandle to ImageList

Posted on 2001-08-03
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
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
  • 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 100 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
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 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…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

628 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