473,722 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using SHGetFileInfo for icons - problems with adding to ImageList

Hello,

I am using the following variation of code from
<http://support.microso ft.com/default.aspx?sc id=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(Unma nagedType.ByVal TStr, SizeConst:=260) > Public
szDisplayName As String
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=80)> Public
szTypeName As String
End Structure

Private Declare Ansi Function SHGetFileInfo Lib "shell32.dl l" (ByVal
pszPath As String, _
ByVal dwFileAttribute s 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(ByV al 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.szDispla yName = New String(Chr(0), 260)
shinfo.szTypeNa me = New String(Chr(0), 80)

'Use this to get the small icon.
hImgSmall = SHGetFileInfo(s FileName, 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.Fr omHandle(shinfo .hIcon)
lvListView.Smal lImageList.Imag es.Add(myIcon)

'Use this to get the large icon.
hImgLarge = SHGetFileInfo(s FileName, 0, shinfo,
Marshal.SizeOf( shinfo), SHGFI_ICON Or SHGFI_LARGEICON )

myIcon = Drawing.Icon.Fr omHandle(shinfo .hIcon)
lvListView.Larg eImageList.Imag es.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.Sma llImageList.Ima ges.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 2863

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

Similar topics

4
10369
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
15089
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 file icon, this isn't showed. I tried to do by two ways: 1- By creating the ListViewItem and adding it to ListView, then I get rectangle of icon element and paint a new icon in rectangle coordenates. I'm able to view icons while they are...
3
7071
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 obtain a CDROM letters? (D:/, E:/ ecc.)
0
332
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 SHFILEINFO Public hIcon As IntPtr ' : icon Public iIcon As Integer ' : icondex Public dwAttributes As Integer ' : SFGAO_ flags
1
3212
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 C# solution web part, and I cannot reference the system.windows.forms reference so I can not access the ListView. So I wanted to see if there was a way to return the Icon from the SHGetFileInfo and display it.
1
1588
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 distorted when I use it in the application (Treeview). What could cause the distortion of the icon. Did I miss some settings. (I'm new to VB.Net)
0
1038
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 get icons for files and folders, it work fine but in same program i use SHGetFileInfo again to get a file type as it is display in windows explorer. this time i make a vb class file and call it whenever i req. to display a file type, but it shows some...
1
2685
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, and at runtime I want to be able to add more icons as I need to add. I don't want to use an imagelist because that will be a set number of icons in the program.
1
1820
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 displays small icon. hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON); hImgLarge = Win32.SHGetFileInfo(fName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON |...
0
8863
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9157
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.