Solved

IconHandle to ImageList

Posted on 2001-08-03
5
542 Views
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...
0
Comment
Question by:Marshow
  • 2
  • 2
5 Comments
 

Expert Comment

by:sdjefferies
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.

Thanks
Simon
0
 

Author Comment

by:Marshow
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.
0
 

Expert Comment

by:sdjefferies
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.
0
 

Author Comment

by:Marshow
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
Dim shInfo As SHFILEINFO

   lRet = SHGetFileInfo(FilePath, 0&, shInfo, Len(shInfo), SHGFI_DISPLAYNAME Or SHGFI_ICON Or SHGFI_SMALLICON Or SHGFI_TYPENAME)
   sDisp = shInfo.szDisplayName
   sType = shInfo.szTypeName
   hIcon = shInfo.hIcon

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

Accepted Solution

by:
Ark earned 100 total points
ID: 6350996
Hi
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))

Cheers
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now