Results 1 to 10 of 10

Thread: file icon to treeNode image

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283

    file icon to treeNode image

    how do i add an icon to a treenode?
    i don't want to necessarily use an imagelist...

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Why would you NOT want to use an imagelist? It may be easier to dynamically add images to the imagelist and then add the treenode using that imageindex.

  3. #3

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    i have a treeview, which is merely a file/folder structure. when i add a file, i want to associate it with its icon.

    so, when i find a file, i add the treenode and then i get the icon associated with the file. if i use an imagelist then i need to see if that icon is already part of the imagelist and find its index... thats tedious.
    if you have a thought how i might more effectively use an imagelist tell me, because right now it dosn't make much sense to me for what i'm trying to do.

  4. #4
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    check this thread out
    this should answer your question.

  5. #5

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    Code:
        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
    
        Public Function ExtractIconFromFile(ByVal Filename As String, ByVal IconIndex As Integer, ByVal IconSize As iSize) As Drawing.Icon
            Dim hIconLarge As IntPtr
            Dim hIconSmall As IntPtr
    
            Extract_Icon(Filename, IconIndex, hIconLarge, hIconSmall, 1)
    
            If IconSize = iSize.Large Then
                Return Drawing.Icon.FromHandle(hIconLarge)
            Else
                Return Drawing.Icon.FromHandle(hIconSmall)
            End If
        End Function
    ok, so now i can get the icon of a file or folder. how do i add that icon to a treenode? i have to first convert it to an image file ifi want to use an imageList...

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You don't need to convert it to a file to add it to an imagelist. It returns an icon object and that can be added directly to theh imagelist at runtime:

    ImageList1.Images.Add(myIcon)

    The only hard part would be finding out if it is already in the list. The easiest way around that would probably be to track which extensions you already have images for:

    Public ht As New HashTable 'class level scope

    ImageList1.Images.Add(myIcon)
    ht.Add(extension, myIcon)
    'or
    ht.Add(extension, myIconIndex)

    Then you could retrieve the image or index via the file extension since it is the key for the hashtable.
    Last edited by Edneeis; Feb 9th, 2004 at 07:25 PM.

  7. #7

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    in the code in my last post, what is iSize.large??? its underlined in my version and not recognized.

  8. #8
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    try putting this in your "imports" section:

    System.Drawing.Icon

    look up the icon class and it may have further infor you could use.

    also, in that post you got the code from, there is a structure that defines iIcon:

    VB Code:
    1. Private Structure SHFileInfo
    2.     Public hIcon As IntPtr
    3.     Public iIcon As Integer
    4.     Public dwAttributes As Integer
    5.     Public szDisplayName As String
    6.     Public szTypeName As String
    7.         End Structure

  9. #9

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    imports system.drawing.icon dosn't help?
    the size class also dosn't have a large property.
    what size is probably meant by large? then i can just compare width and height properties...

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

    iSize is just a homemade enum, to determine if the function should return the large or the small icon.
    That is why your VB doesn't recognize it.

    If you now you always want the small just remove the if statement and use the Return Drawing.Icon.FromHandle(hIconSmall)
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

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