
ExtractIcon with a VB ImageList Control
Samuel,
Quote:
> I have a standard ImageList Control in VB 5 and I need to populate it at
> runtime with images. OK this is easy using LoadRes... whatever, but I need
> to extract icons from the system dll's eg system32.dll. How can I do
> this???
> I know you can use API calls (ExtractIcon) to get an icon, but this only
> returns a handle to the icon and the imagelist control cannot accept this.
> Can I coerce it??
:-) Yeah, only if you ask it real nicely. Actually try the code below that
converts an icon handle to a picture object which can then be passed
as the Picture param of a ListImage's Add method. BTW it's a derivation
of the code found in MS KB article ID Q141933 (or Q161299) "HOWTO:
VB4: Capture and Print the Screen, a Form, or any Window". Sorry,
don't have the URL...
' ============================================
' Code module declares
Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Type PICTDESC
cbSize As Long
picType As Long ' one of VB's PictureTypeConstants
hImage As Long ' HBITMAP, HMETAFILE, HICON, HENHMETAFILE
Data1 As Long ' HBITMAP hpal, HMETAFILE xExt
Data2 As Long ' HMETAFILE yExt
End Type
' Note that IPicture is a hidden interface implemented by the
' OLE Automation type library Stdole2.tlb. You will need to add
' a project reference to the typelib in order to use this function.
Declare Function OleCreatePictureIndirect Lib "olepro32.dll" _
(pPictDesc As PICTDESC, _
RefIID As GUID, _
ByVal fPictureOwnsHandle As Long, _
ppvObj As IPicture) As Long
Public Const S_OK = 0 ' indicates successful HRESULT
' ============================================
' Creates a picture object from an image handle
' hImage - either a HBITMAP, HMETAFILE, HICON or HENHMETAFILE
' dwPicType - one of VB's PictureTypeConstants describing the hImage param.
' Returns a VB Picture object on success, or the object value "Nothing" otherwise.
Public Function GetPicture(hImage As Long, _
dwPicType As PictureTypeConstants) As Picture
Dim pd As PICTDESC
Dim IID_IDispatch As GUID
Dim objPic As Picture
If hImage = 0 Then Exit Function
' Fill the picture description struct
With pd
.cbSize = Len(pd) ' must specify the struct's size
.picType = dwPicType ' vbPicTypeIcon
.hImage = hImage
End With
' Fill the IDispatch Interface ID, {00020400-0000-0000-C000-000000046}
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
' Create the Picture object
If OleCreatePictureIndirect(pd, IID_IDispatch, True, objPic) = S_OK Then
Set GetPicture = objPic
End If
End Function
Quote:
> I have tried creating an ImageList in code using comclt32 API calls, but I
> can not then get the common controls (treeview/listview) to accept this as
> a valid ImageList Object. I am sure this can be done, but what am I
> missing?
Not so sure. Comctl32.ocx's imagelist is quite a different beast than
the imagelist control itself. As far as I know, Comctl32.ocx's listview
control will only talk to Comctl32.ocx's imagelist control. But if by
change you do figure something out, please post it here...
--
Brad Martinez
http://members.aol.com/btmtz/vb
Visit the new CCRP site at:
http://www.mvps.org/ccrp
Please direct questions/replies to the newsgroup.