Results 1 to 10 of 10

Thread: folder icons

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283

    folder icons

    is there a way to get the icon for a specific folder (if it has been customized in windows)?
    and get icons for things like my computer / desktop / my documents / ???

    thanks.

  2. #2
    Lively Member
    Join Date
    Dec 2003
    Posts
    91
    like to use on your form? search for *.ico under all files/folders... you should find all icons saved on ur comp then...

  3. #3
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    This should work both for files and folders.
    Just specify the path for the folder as filename.

    VB Code:
    1. Private Declare Auto 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
    2.  
    3.        Private Const SHGFI_ICON = &H100
    4.        Private Const SHGFI_LARGEICON = &H0
    5.        Private Const SHGFI_USEFILEATTRIBUTES = &H10
    6.  
    7.        Private Structure SHFileInfo
    8.     Public hIcon As IntPtr
    9.     Public iIcon As Integer
    10.     Public dwAttributes As Integer
    11.     Public szDisplayName As String
    12.     Public szTypeName As String
    13.         End Structure
    14.  
    15.         Public Function ExtractIcon(ByVal Filename As String) As Drawing.Icon
    16.             Dim sh As New SHFileInfo()
    17.             Dim hwnd As IntPtr
    18.  
    19.             hwnd = SHGetFileInfo(Filename, 0, sh, Marshal.SizeOf(sh), SHGFI_ICON Or SHGFI_LARGEICON Or SHGFI_USEFILEATTRIBUTES)
    20.  
    21.             hwnd = Nothing
    22.  
    23.             Return Drawing.Icon.FromHandle(sh.hIcon)
    24.         End Function
    Last edited by pax; Feb 6th, 2004 at 06:12 PM.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  4. #4
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    how do we need to return this? ifn a textfle? message box?

  5. #5
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Originally posted by thephantom
    how do we need to return this? ifn a textfle? message box?
    I'm not sure I understand the question?!?

    If you are refering to the function above, it just returns a System.Drawing.Icon object.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  6. #6

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    i want it so that i can add it to a treenode as the image.
    i think i need to use imagelist for that...
    haven't looked into it yet, since i didn't know if it was possible to get folder icons...
    now that i know how, i'll work on implementing it...

    thanks for the code pax.

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Why would you declare the struct private, but all it's members public?

  8. #8
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    Originally posted by pax
    I'm not sure I understand the question?!?

    If you are refering to the function above, it just returns a System.Drawing.Icon object.
    I was trying to figure out how to use the function. Like, what would call the procedure? How do I use the return value? Is the return used as a property? like example: imageOfSomething = {the procedure call here}

    I also can't figure out what folder or file you mention as the path. would that be a .dll such as system32.dll or similar that houses many icons?

    can't really explain what I don't know very well lol

  9. #9
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Oh, I think I get now...

    To get the icon for a JPG image for example just call the function ExtractIcon("*.jpg")

    That will return the same icon you'd expect to see in the explorer for a JPG image.

    You could also call it with a folder like ExtractIcon("C:\Temp")
    That should return the same icon as the C:\Temp folder has in the explorer.

    This function can't get the icons from a icon resource file like DLL.
    You need to use the ExtractIcon API for that, cos' you need to specify an index in case there are multiple icons.

    I think it would be something like this:
    VB Code:
    1. Private Declare Function Extract_Icon Lib "shell32.dll" Alias "ExtractIconEx" (ByVal lpszExeFileName As String, ByVal nIconIndex As Integer, ByRef hIconLarge As IntPtr, ByRef hIconSmall As IntPtr, ByVal IconCount As Integer) As IntPtr
    2.  
    3.     Public Function ExtractIconFromFile(ByVal Filename As String, ByVal IconIndex As Integer, ByVal IconSize As iSize) As Drawing.Icon
    4.         Dim hIconLarge As IntPtr
    5.         Dim hIconSmall As IntPtr
    6.  
    7.         Extract_Icon(Filename, IconIndex, hIconLarge, hIconSmall, 1)
    8.  
    9.         If IconSize = iSize.Large Then
    10.             Return Drawing.Icon.FromHandle(hIconLarge)
    11.         Else
    12.             Return Drawing.Icon.FromHandle(hIconSmall)
    13.         End If
    14.     End Function
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  10. #10
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Wink

    I see now, that makes a lot of sense.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width