[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:
Dim nodeNew As New TreeNode
nodeNew.Text = "C:\"
nodeNew.Tag = "C:\"
Dim MyIcon As Drawing.Bitmap = Drawing.Icon.ExtractAssociatedIcon("C:\").ToBitmap()
'The line above gives me a file-not-found error.
Dim imlIcons As New ImageList
imlIcons.Images.Add(MyIcon)
nodeNew.ImageIndex = imlIcons.Images.Count - 1
tvwFolder.ImageList = imlIcons
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?
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.
Re: [2005] ExtractAssociatedIcon for folders?
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.
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.
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/
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:
Dim myIntPtr As New IntPtr(myInteger)
Dim myInt32 As Integer = myIntPtr.ToInt32()
Re: [2005] ExtractAssociatedIcon for folders?
I edited the code from CodeGuru and now it works fine.
Declarations:
VB Code:
Private Const MAX_PATH = 260
Private Structure SHFILEINFO
Public hIcon As IntPtr 'out: icon
Public iIcon As Long 'out: icon index
Public dwAttributes As Long 'out: SFGAO_ flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)> _
Public szDisplayName As String 'out: display name (or path)
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String 'out: type name
End Structure
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
Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Extraction code:
VB Code:
Dim shInfo As SHFILEINFO
shInfo = New SHFILEINFO()
shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
shInfo.szTypeName = New String(vbNullChar, 80)
Dim FileName As String = "C:\"
Dim hIcon As IntPtr
hIcon = SHGetFileInfo(FileName, 0, shInfo, Marshal.SizeOf(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
Dim MyIcon As Drawing.Bitmap
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.