473,375 Members | 1,298 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,375 software developers and data experts.

Using SHGetFileInfo for icons - problems with adding to ImageList


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)

'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)
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

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?

- Andre
Nov 21 '05 #1
0 2798

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

Similar topics

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...
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...
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...
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...
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...
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...
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...
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,...
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...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.