Results 1 to 8 of 8

Thread: [RESOLVED] [2005] ExtractAssociatedIcon for folders?

  1. #1

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Resolved [RESOLVED] [2005] ExtractAssociatedIcon for folders?

    Hello everyone,

    I am making an application that has a treeview control that should show the drives and their folders of the user's system. To make this a good overview, it would be nice if the icons that go with the folder or drive are shown in the treeview.

    Drawing.Icon.ExtractAssociatedIcon works great for files, but tells me 'File not found' when I try to extract the icon of a folder.

    This is the code I have now to add the C-drive's treeviewnode:
    VB Code:
    1. Dim nodeNew As New TreeNode
    2. nodeNew.Text = "C:\"
    3. nodeNew.Tag = "C:\"
    4.  
    5. Dim MyIcon As Drawing.Bitmap = Drawing.Icon.ExtractAssociatedIcon("C:\").ToBitmap()
    6. 'The line above gives me a file-not-found error.
    7.  
    8. Dim imlIcons As New ImageList
    9. imlIcons.Images.Add(MyIcon)
    10. nodeNew.ImageIndex = imlIcons.Images.Count - 1
    11.  
    12. tvwFolder.ImageList = imlIcons
    13. tvwFolder.Nodes.Add(nodeNew)
    Is there a way in VB.NET to extract icons from folders and drives?

    Thanks for your help,

    Alexander.

    P.S. On a side note, is there a way to set the image for a treeview node without the use of an imagelist?
    Last edited by arsmakman; Jul 15th, 2006 at 07:49 AM.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] ExtractAssociatedIcon for folders?

    I haven't actually heard of ExtractAssociatedIcon, I've always assumed that you'd use SHGetFileInfo to get the icons for files as well as folders. Perhaps that's what you need to use.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] ExtractAssociatedIcon for folders?


  4. #4

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: [2005] ExtractAssociatedIcon for folders?

    Thanks Mendhak, I'll try the SHGetFileInfo API.

    The links you gave me shows how to add the icons using an imagelist control, I would like to find out if it can be done without one.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] ExtractAssociatedIcon for folders?

    If you read the help for ExtractAssociatedIcon it specifically says that the argument is "The path to the file that contains an image.". Folders and drives are not files and they do not contain images. Windows uses images form other files to represent them in Explorer. ExtractAssociatedIcon will only get an icon contained in the file that you specify.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: [2005] ExtractAssociatedIcon for folders?

    Pfft, this SHGetFileInfo API works very difficultly.
    I copied the declaration, types and constants from VB6.0's 'API Text Viewer', but I can't get it to work properly in .NET.

    Another problem is that the APi returns the handle of the icon as a Long, while Vb .NET only accept handles as an IntPtr.

    I'm now looking for a working example of the API written in VB.NET...

    EDIT: Found one - http://www.codeguru.com/vb/gen/vb_mi...cle.php/c5597/
    Last edited by arsmakman; Jul 15th, 2006 at 08:50 AM.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] ExtractAssociatedIcon for folders?

    If you're converting VB6 API declarations to VB.NET then simply change all the Longs to Integers. Also, if a parameter is a handle then you can simply change the Integer to a IntPtr, otherwise you can convert between the two very easily.
    VB Code:
    1. Dim myIntPtr As New IntPtr(myInteger)
    2. Dim myInt32 As Integer = myIntPtr.ToInt32()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: [2005] ExtractAssociatedIcon for folders?

    I edited the code from CodeGuru and now it works fine.

    Declarations:
    VB Code:
    1. Private Const MAX_PATH = 260
    2.  
    3. Private Structure SHFILEINFO
    4.     Public hIcon As IntPtr 'out: icon
    5.     Public iIcon As Long 'out: icon index
    6.     Public dwAttributes As Long 'out: SFGAO_ flags
    7.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)> _
    8.         Public szDisplayName As String 'out: display name (or path)
    9.     <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
    10.         Public szTypeName As String 'out: type name
    11. End Structure
    12.  
    13. Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
    14.     (ByVal pszPath As String, _
    15.     ByVal dwFileAttributes As Integer, _
    16.     ByRef psfi As SHFILEINFO, _
    17.     ByVal cbFileInfo As Integer, _
    18.     ByVal uFlags As Integer) As IntPtr
    19.  
    20. Private Const SHGFI_ICON = &H100
    21. Private Const SHGFI_SMALLICON = &H1
    Extraction code:
    VB Code:
    1. Dim shInfo As SHFILEINFO
    2. shInfo = New SHFILEINFO()
    3. shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
    4. shInfo.szTypeName = New String(vbNullChar, 80)
    5.  
    6. Dim FileName As String = "C:\"
    7. Dim hIcon As IntPtr
    8. hIcon = SHGetFileInfo(FileName, 0, shInfo, Marshal.SizeOf(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
    9.  
    10. Dim MyIcon As Drawing.Bitmap
    11. MyIcon = Drawing.Icon.FromHandle(shInfo.hIcon).ToBitmap
    It's also possible to get the icon for an 'open' folder by using the SHGFI_SELECTED constant (= &H10000).

    Thanks for the tip, Mendhak!

    And thank for the IntPtr convertion tip, jmcilhinney.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

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