I am building an "Explorer" type view of files using the Listview control.
I would like it to display not only the file's name, etc, but also its
associated icon. To use icons in the Listview control they must come from
an ImageList control. I can extract the associated icon and display it in a
PictureBox control. My trouble is that when I try to add the picture to an
ImageList control I get an "Out of memory" error. I suspect that perhaps my
original picture extracted from the associated icon is somehow corrupt but
that the PictureBox control overcomes it well enough to display the image,
however the ImageList control can't overcome it and fails. Included below
are the function I am using to extract the associated icon and the notation
I am using in my attempt to add it to the ImageList. If anyone has any
ideas about what I could be doing wrong, or should be doing, or ???
Any and all help truly appreciated.
Thanks, Jon Anderson
Set Picture1.Picture = GetIconForFile("c:\foo.html") ' works - IE icon
Set o = ImageList1.ListImages.Add(,,GetIconForFile("c:\foo.html")) '
fails - Out of memory
Set o = ImageList1.ListImages.Add(,,Picture1.Picture) ' also fails - Out
Public Function GetIconForFile(ByVal sFileName As String) As IPictureDisp
Dim hIcon As Long
Dim Pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID
hIcon = ExtractAssociatedIcon(App.hInstance, sFileName, 2)
If (hIcon > 0) Then
' Fill in with IDispatch Interface ID.
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
' Fill Pic with necessary parts.
.Size = Len(Pic) ' Length of structure.
.Type = vbPicTypeIcon ' Type of Picture (bitmap).
.hBmp = hIcon ' Handle to bitmap.
' Create Picture object.
Call OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
' Return the new Picture object.
Set GetIconForFile = IPic