-
Jul 28th, 2009, 02:42 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Icon.ExtractAssociatedIcon and shell32.dll
I'm not sure exactly if the folder icon is in shell32.dll, but nonetheless if it is.
Basically, whenever I come across a folder in a FileSystem list, I want to add the icon for a folder to the image list that I am using for the ListView. The problem is that I'm not sure how to reference a specific icon location in a icon collection in a dll.
Is this possible with Icon.ExtractAssociatedIcon and shell32.dll?
Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7
SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
[Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]
[.NET and MySQL Quick Guide]
-
Jul 28th, 2009, 10:32 PM
#2
Thread Starter
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7
SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
[Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]
[.NET and MySQL Quick Guide]
-
Jul 28th, 2009, 11:41 PM
#3
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
Yeah... It is possible... From what I understand, you want to display the icon associated to an specific file, is that right? If you want to do so, just do something like this: (unless it's a folder, in which case you will have to extract the folder icon from shell32.dll).
If you are using an OpenFileDialog you might want to try this: (You have to set the large image list source from the ListView to ImageList1, and then the ImageList properties to: Image size 32;32, Image Bit Depth: 32Bit)
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click OpenFileDialog1.ShowDialog() ImageList1.Images.Add(Icon.ExtractAssociatedIcon(OpenFileDialog1.FileName)) 'If you want to add several files at the same time then you should change the image index to a variable... 'You know what's next... ListView1.Items.Add(IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName), imageIndex:=(0)) 'If you don't want to use a loop, just clean the image list... ImageList1.Images.Clear() 'This assumes that you are adding ONE file at a time... End Sub
Forgot to mention... This one time I needed the folder icon, so I used a IcoFX to extract the icons from shell32.dll and find the index of the folder icon... I think it's four. I did this because I didn't know how to extract icons programmatically. Might be helpful.
Last edited by tassa; Jul 28th, 2009 at 11:45 PM.
-
Jul 28th, 2009, 11:45 PM
#4
Thread Starter
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
No, I said I want the image a folder.
I'm not sure where the actual folder icon is stored (eg shell32.dll) but I thought that's where it was.
I already have all the code to get icons for files, but what to do about folders? I applied my own icon, but it gets rendered like crap when placed into the ListView.
I was hoping that maybe somehow extracting an icon for a folder from somewhere would return a better quality image.
Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7
SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
[Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]
[.NET and MySQL Quick Guide]
-
Jul 28th, 2009, 11:55 PM
#5
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
Hey, I was looking into that for you...
Yes, the icon folder IT IS stored within shell32.dll, however, this file contains over 200 icons, that's why when you get the associated icon you must specify which one you want.
I had done this:
vb.net Code:
For i as integer = 0 to 237 ImageList1.Images.Add(Icon.ExtractAssociatedIcon("shell32.dll")) Next i
Now you have all 238 icons in a ImageList. If you want the folder icon just get the image at index #4. It's the same index in XP and Vista.
-
Jul 29th, 2009, 12:03 AM
#6
Thread Starter
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
oh, i get it. it will put all of them in there automatically.
OK, thanks
Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7
SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
[Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]
[.NET and MySQL Quick Guide]
-
Jul 29th, 2009, 11:47 AM
#7
Thread Starter
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
Hmm, actually, that's not right. There is nothing there to tell it to iterate through each icon in shell32.dll, it simply extracts shell32.dll's associated icon, 236 times.
It isn't an icon extractor in the sense it can grab any icon, it can only grab an icon associated with that file.
It seems I might have to find another way or use that stupid huge API code people have written to do it.
Oh well.
Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7
SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
[Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]
[.NET and MySQL Quick Guide]
-
Jul 29th, 2009, 01:26 PM
#8
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
Seraph, sorry, I have given you the wrong code, this code will extract the folder icon from shell32.dll
vb.net Code:
Dim file As String = ("c:\windows\system32\shell32.dll") Dim iconNumber As Integer = 3 Dim ico As Icon = Icon.FromHandle(ExtractIcon(Me.Handle, file, iconNumber)) Me.ImageList1.Images.Add(ico.ToBitmap)
After you extract it, just add the ImageList image to ListView and presto!
-
Jul 29th, 2009, 01:37 PM
#9
Thread Starter
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7
SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
[Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]
[.NET and MySQL Quick Guide]
-
Jul 29th, 2009, 01:39 PM
#10
Thread Starter
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
It doesn't like ExtractIcon.
Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7
SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
[Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]
[.NET and MySQL Quick Guide]
-
Jul 29th, 2009, 04:58 PM
#11
Re: Icon.ExtractAssociatedIcon and shell32.dll
ExtractIcon is an API declared in shell32.dll. You need to use the API to get the icon because that's the only way you can specify the index of the icon to extract.
-
Jul 30th, 2009, 09:35 AM
#12
Thread Starter
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
OK, so here's what I have now:
Code:
Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal nIconIndex As Int32, ByRef phiconLarge As IntPtr, ByRef phiconSmall As IntPtr, ByVal nIcons As Int32) As Int32
Dim icoLarge As IntPtr
Dim icoSmall As IntPtr
Dim file As String = ("%SystemRoot%\system32\shell32.dll")
ExtractIcon(file, 3, icoLarge, icoSmall, 1)
Dim ico As Icon = Icon.FromHandle(icoSmall)
The problem now is that the icons that are being put into the ListView are super crappy quality. Is that because it is extracting the crappy ones or because the ListView is screwing with them? I even switched to icoLarge and same result.
Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7
SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
[Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]
[.NET and MySQL Quick Guide]
-
Jul 30th, 2009, 09:47 AM
#13
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
I had to retrieve icons for every kind of file. I got this from someone on these forums and edited it so that I could also retrieve the folder icon. I know you were already getting the icon, but maybe this could help with quality. I don't know.
Make a new class with this:
Public Enum IconSize
Small
Large
End Enum
Public Class IconExtractor
'----------------------------------------------------------------------------
'
' Description: Extracts the icon associated with any file on your system.
' Author: WidgetMan http://softwidgets.com
'
' Remarks...
'
' Class requires the IconSize enumeration that is implemented in this
' same file. For best results, draw an icon from within a control's Paint
' event via the e.Graphics.DrawIcon method.
'
'----------------------------------------------------------------------------
Private Const SHGFI_ICON As Integer = &H100
Private Const SHGFI_SMALLICON As Integer = &H1
Private Const SHGFI_LARGEICON As Integer = &H0
Private Const SHGFI_USEFILEATTRIBUTES As Integer = &H10
Private Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As Integer
<VBFixedString(260), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<VBFixedString(80), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure
Private Declare Function SHGetFileInfo Lib "shell32" Alias "SHGetFileInfoA" _
(ByVal pszPath As String, _
ByVal dwFileAttributes As Integer, _
ByRef psfi As SHFILEINFO, _
ByVal ByValcbFileInfo As Integer, _
ByVal uFlags As Integer) As Integer
Public Sub New()
End Sub
Public Overloads Function Extract(ByVal File As String, ByVal Size As IconSize, Optional ByVal UseFileAttributes As Integer = 1) As System.Drawing.Icon
Dim aSHFileInfo As New SHFILEINFO
Dim cbFileInfo As Integer
Dim uflags As Integer
Dim Icon As System.Drawing.Icon
Select Case Size
Case IconSize.Large
uflags = SHGFI_ICON Or SHGFI_LARGEICON
Case Else
uflags = SHGFI_ICON Or SHGFI_SMALLICON
End Select
'ORing with SHGFI_USEFILEATTRIBUTES will retrieve the icon even if it the file doesn't
'exist (x.txt), however, it won't retrieve the folder icon. So, it isn't used when
'retrieving a folder icon by passing in a 0 for UseFileAttributes (optional parameter)
If UseFileAttributes Then uflags = uflags Or SHGFI_USEFILEATTRIBUTES
cbFileInfo = Marshal.SizeOf(aSHFileInfo)
SHGetFileInfo(File, 0, aSHFileInfo, cbFileInfo, uflags)
Icon = System.Drawing.Icon.FromHandle(aSHFileInfo.hIcon)
Return Icon
End Function
Public Overloads Function Extract(ByVal File As String) As System.Drawing.Icon
Return Me.Extract(File, IconSize.Small)
End Function
End Class
Extract with this:
Dim IconEx As New IconExtractor
IconEx.Extract(Application.StartupPath, IconSize.Small, 0).ToBitmap
VB.Net 2008
.Net Framework 2.0
"Must you breathe? 'Cause I need heaven..."
-
Jul 30th, 2009, 09:51 AM
#14
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
Oh, and more to the point, I was putting my icons in a treeview. They looked a little crappy, too, until I changed the ImageList to hold higher res images. Then they looked nice.
VB.Net 2008
.Net Framework 2.0
"Must you breathe? 'Cause I need heaven..."
-
Jul 30th, 2009, 09:58 AM
#15
Thread Starter
Fanatic Member
Re: Icon.ExtractAssociatedIcon and shell32.dll
Nah, same quality.
Guess I'll just have to deal with it.
I think it may be the ListView though.
Because images I know look decent at 16x16 are looking horrible in the ListView.
I have the DevXpress controls at home. Maybe their LsitView will support better quality.
Thanks for all the code tho.
Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7
SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
[Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]
[.NET and MySQL Quick Guide]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|