473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Using SHGetFileInfo for icons - problems with adding to ImageList

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
Nov 21 '05 #1
0 2804

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: What-a-Tool | last post by:
Is possible to use the windows image list for icon display in my treeview control? Would appreciate it you could point me towards a code sample or Key phrase to use in a google search. Thanks...
1
by: Víctor | last post by:
I'm doing a kind of file explorer with some additional funcionalities. The problem is that I'm not able to draw file extension icons in a ListView. I know how obtain icons, but when I try to draw...
3
by: Giuseppe Pezzella | last post by:
Hi In Windows every file have yours icon, so: how I get them ( Extension and Icon) and save in a imageList for use later in a ListView? In VB6 was SHGetFileInfo. In VB.NET? How I can...
0
by: Andre Viens | last post by:
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...
1
by: AdrEsqu | last post by:
I am using the SHGetFileInfo to return the system icon. Is there anyway to return and display the icon with out having to return it to the ListView? I'm using the SHGetFileInfo in my SharePoint...
1
by: Manfred | last post by:
Hello I added some icons to an imagelist. I checked first the image size and image depth and adjusted the settings according in the imagelist before adding the icon. The icon still gets...
0
Blade
by: Blade | last post by:
Hello Friends well this is my first post on this site, hope i get the solution for my problem During the development of one project i am facing this problem i use a SHGetFileInfo structure to...
1
by: John | last post by:
Is there such a thing as storing icons or bitmaps into a resource file, and adding more icons or bitmaps into the resource file at runtime? I want to store a bunch of icons into a resource file,...
1
by: Nilam2477 | last post by:
I need to use SHGetFileInfo to display large icons.For small icon display it works fine. If i change the parameter to display large icon it does not display the file with large icon, it always...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.