VS 2008 [RESOLVED] Icon.ExtractAssociatedIcon and shell32.dll-VBForums
Results 1 to 15 of 15

Thread: [RESOLVED] Icon.ExtractAssociatedIcon and shell32.dll

  1. #1

    Thread Starter
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    Resolved [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]

  2. #2

    Thread Starter
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    Re: Icon.ExtractAssociatedIcon and shell32.dll

    /bump

    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]

  3. #3
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         OpenFileDialog1.ShowDialog()
    3.         ImageList1.Images.Add(Icon.ExtractAssociatedIcon(OpenFileDialog1.FileName))
    4.         'If you want to add several files at the same time then you should change the image index to a variable...
    5.         'You know what's next...
    6.         ListView1.Items.Add(IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName), imageIndex:=(0))
    7.         'If you don't want to use a loop, just clean the image list...
    8.         ImageList1.Images.Clear()
    9.         'This assumes that you are adding ONE file at a time...
    10.     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.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  4. #4

    Thread Starter
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    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]

  5. #5
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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:
    1. For i as integer = 0 to 237
    2. ImageList1.Images.Add(Icon.ExtractAssociatedIcon("shell32.dll"))
    3. 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.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  6. #6

    Thread Starter
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    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]

  7. #7

    Thread Starter
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    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]

  8. #8
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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:
    1. Dim file As String = ("c:\windows\system32\shell32.dll")
    2.             Dim iconNumber As Integer = 3
    3.             Dim ico As Icon = Icon.FromHandle(ExtractIcon(Me.Handle, file, iconNumber))
    4.             Me.ImageList1.Images.Add(ico.ToBitmap)

    After you extract it, just add the ImageList image to ListView and presto!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  9. #9

    Thread Starter
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    Re: Icon.ExtractAssociatedIcon and shell32.dll

    Haha. OK, thanks. lol

    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]

  10. #10

    Thread Starter
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    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]

  11. #11
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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.

  12. #12

    Thread Starter
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    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]

  13. #13
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    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..."

  14. #14
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    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..."

  15. #15

    Thread Starter
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.