Hello,
I am using the following variation of code from
<http://support.microsoft.com/default.aspx?scid=kb;EN-US;319340> to add icons
to an imagelist for use in a listview:
Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public
szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public
szTypeName As String
End Structure
Private Declare Ansi Function SHGetFileInfo Lib "shell32.dll" (ByVal
pszPath As String, _
ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal
cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr
Private Const SHGFI_ICON As Integer = &H100
Private Const SHGFI_SMALLICON As Integer = &H1
Private Const SHGFI_LARGEICON As Integer = &H0 ' Large icon
Sub IconExtract(ByVal sFileName As String, ByRef lvListView As ListView)
'This sub takes the filename of the icon to extract and a reference
to the listview
'to add the large and small icons to...
Dim hImgSmall As IntPtr 'The handle to the system image list.
Dim hImgLarge As IntPtr 'The handle to the system image list.
Dim fName As String 'The file name to get the icon from.
Dim shinfo As SHFILEINFO
shinfo = New SHFILEINFO
shinfo.szDisplayName = New String(Chr(0), 260)
shinfo.szTypeName = New String(Chr(0), 80)
'Use this to get the small icon.
hImgSmall = SHGetFileInfo(sFileName, 0, shinfo,
Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)
'The icon is returned in the hIcon member of the shinfo struct.
Dim myIcon As Drawing.Icon = Drawing.Icon.FromHandle(shinfo.hIcon)
lvListView.SmallImageList.Images.Add(myIcon)
'Use this to get the large icon.
hImgLarge = SHGetFileInfo(sFileName, 0, shinfo,
Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)
myIcon = Drawing.Icon.FromHandle(shinfo.hIcon)
lvListView.LargeImageList.Images.Add(myIcon)
End Sub
I call the subroutine IconExtract and pass it the full path and filename of
the icon I want to extract, and a reference (note the ByRef) to my listview.
I want to extract the icons for the file and add it to the appropriate Small
or Large imagelist on the existing listview.
This is my problem... it doesn't work. The code gets to the first line of
adding the icon to the imagelist
(lvListView.SmallImageList.Images.Add(myIcon)) and just seems to stop. When
in debug, the program jumps back to the foreground and allows me to keep
executing it, but the subroutine never completed. There is also no error
thrown.
I'm sure the strangeness I'm experiencing is due to the Interop I'm
utilizing to get the icons, but why does the addition of the icon fail on an
existing imagelist? If I don't use the reference to my existing listview and
rather initialize a new ImageList and add the icon to it, it then works. Then
I can reference that new image list to the imagelist in my listview, except I
have existing images in my imagelist on my listview, so I just want to add to
them, and that's where my problem lies.
Is this a bug with dealing with the Interop or something I'm doing wrong?
Thanks.
- Andre