Would it be easy to extract an icon from a file, then add it to an ImageList for use by TreeView control?
Printable View
Would it be easy to extract an icon from a file, then add it to an ImageList for use by TreeView control?
Yep, it is, just download a control from vbaccelerator.com called Icon Extractor and extract the icon, then you could put it in any Imagelist like you always did.
Note to self: be more specific in VB forums.
I mean during execution of my application.
Ok, you need to have a treeview, a imagelist and a picturebox (hidden with autoredraw on)
Code:'In a module
Declare Function ExtractIconEx Lib "shell32" Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal nIconIndex As Long, phIconLarge As Long, phIconSmall As Long, ByVal nIcons As Long) As Long
Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hicon As Long) As Long
Function GetIconsFromFile(Filename$)
Dim hIcons()
ReDim hIcons(ExtractIconEx(Filename, -1, ByVal 0&, ByVal 0&, 0))
If UBound(hIcons) = 0 Then Exit Function Else ReDim Preserve hIcons(UBound(hIcons) - 1)
ExtractIconEx Filename, 0, hIcons(0), ByVal 0&, 10
GetIconsFromFile = hIcons
End Function
Function GetIconFromFile&(Filename$, Optional index&)
ExtractIconEx Filename, index, GetIconsFromFile, ByVal 0&, 10
End Function
'In code
DrawIcon picture1.hdc, 0, 0, GetIconFromFile(YourFilepath)
ImageList1.ListImages.Add , "YourIcon", picture1.Picture
TreeView1.Nodes.Add , , , "Caption", "YourIcon"