Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

704 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