Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Problems With ExtractIcon API & Treeview In VB.NET...

How's it going!

I am creating a program that consists of a TreeView that holds string file values that I set as its Nodes.

I have created a Public Function in a Module using the ExtractIcon API from Shell32.dll.

Everything works except for 2 factors

The Icons Return with a nasty black lining in front of them

Anytime I select the 2nd item in the TreeView, it changes to the 1st Items icon.

:confused:

Well Heres the Code

#Region "< Add The System Icon To The Corresponding TreeNode >"

#Region "< Private API Import Method >"

<DllImport("shell32.dll", CallingConvention:=CallingConvention.Winapi, EntryPoint:="ExtractIconA")> _
Private Function ExtractIcon(ByVal hInst As Integer, ByVal lpszExeFileName As String, ByVal nIconIndex As Integer) As IntPtr
End Function

#End Region

#Region "< Public GetCurrentIcon Event >"

Public Function GetCurrentIcon(ByVal Findform As Form, ByVal TreeView As TreeView, ByVal ImageList As ImageList)

Dim oNode As TreeNode
Dim HwndICO As IntPtr
Dim fi As FileInfo

With ImageList
.ColorDepth = ColorDepth.Depth32Bit
.TransparentColor = Color.Transparent
.ImageSize = New Size(32, 32)
End With

TreeView.ImageList = ImageList

Debug.WriteLine("Initiating ''GetCurrentIcon''")

For Each oNode In TreeView.Nodes
fi = New FileInfo(oNode.Text)
If fi.Attributes = 16 Then
Debug.WriteLine(oNode.Text & " is a dir.")
HwndICO = ExtractIcon(Findform.Handle.ToInt32, "C:\WINDOWS\system32\SHELL32.dll", 4)
ImageList.Images.Add(Icon.FromHandle(HwndICO))
oNode.ImageIndex = oNode.Index
ElseIf fi.Attributes <> 16 Then
Debug.WriteLine(oNode.Text & " is not a dir.")
HwndICO = ExtractIcon(Findform.Handle.ToInt32, oNode.Text, 0)

ImageList.Images.Add(Icon.FromHandle(HwndICO))
oNode.ImageIndex = oNode.Index
End If
Next

Debug.Flush()

End Function

#End Region

#End Region

In my Form I have

tvTools.Nodes.Add("C:\Windows\")
tvTools.Nodes.Add("C:\Program Files\Windows NT\Accessories\wordpad.exe")

GetCurrentIcon(Me, tvTools, imglstTMenu)

I am using it like so without "oNode.FullPath" because I want to call items from different directories into one list together.

I also only want to call certain Files & Folders instead of a whole Dir or Drive contents like the Windows Explorer.

Thanks ahead of time! :)

Jugg
[2988 byte] By [Jugg] at [2007-11-11 8:19:03]
# 1 Re: Problems With ExtractIcon API & Treeview In VB.NET...
Hows It going all! :WAVE:

I am still working on this one, I was thinking that it might be the way that GetCurrentIcon was coded, so I incorporated this ExtractIcon API example (http://www.dotnetforums.net/showthread.php?t=92021) from Extreme .NET to check & see, like so

Dim oNode As TreeNode

For Each oNode In tvTools.Nodes
Dim fi = New FileInfo(oNode.Text)

If fi.Attributes = 16 Then
imglstTMenu.Images.Add(GetIcon("C:\WINDOWS\system32\SHELL32.dll, 4"))
oNode.ImageIndex = oNode.Index
Else
imglstTMenu.Images.Add(GetIcon(oNode.Text & ",0"))
oNode.ImageIndex = oNode.Index
End If
Next

I still get the same results though. Is it the For\Each statement or is it the use of FileInfo?

I am still stuck If someone knows of another way of going about this, please let me know.

If I find something or figure out a fix, I will post for future reference.

Have a good one!

Jugg
Jugg at 2007-11-11 21:47:44 >
# 2 Re: Problems With ExtractIcon API & Treeview In VB.NET...
I should have seen this! :o :)

I forgot to add...

oNode.SelectedImageIndex = oNode.Index

In both FileInfo statements inside the For\Each. Something simple, go figure. :D

One down, now off to look into figuring out how to maybe render them up a little

jugg
Jugg at 2007-11-11 21:48:44 >
# 3 Re: Problems With ExtractIcon API & Treeview In VB.NET...
How's It Going All! :WAVE:

I should have seen this one to as I use it to spiff up the ol' app & totaly spaced putting it in... Enable VisualStyles! Go Figure...

Anyone new to this method check out this MSDN article (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsapplicationclassenablevisualstylestopic.asp).

Well I modded out GetCurrentIcon() to use a ListView instead as I work better with them & I was getting problems with the TreeView's ChildNodes & I didn't feel like diving into it so... :rolleyes:

Here's the modded code incase anyone wants to use it further...

Public Function GetCurrentIcon(ByVal Findform As Form, ByVal ListView As ListView, ByVal ImageList As ImageList)

Dim oItem As ListViewItem
Dim HwndICO As IntPtr
Dim fi As FileInfo

With ImageList
.ColorDepth = ColorDepth.Depth32Bit
.TransparentColor = Color.Transparent
.ImageSize = New Size(32, 32)
End With

ListView.LargeImageList = ImageList
ListView.SmallImageList = ImageList
ListView.StateImageList = ImageList

Debug.WriteLine("Initiating ''GetCurrentIcon''")
Dim i As Integer

For Each oItem In ListView.Items

fi = New FileInfo(oItem.Text)

If fi.Attributes = 16 Then
Debug.WriteLine("The Item @ " & oItem.Index & " is a dir.")
HwndICO = ExtractIcon(Findform.Handle.ToInt32, "C:\WINDOWS\system32\SHELL32.dll", 4) '/// if you change the 0 to 1 , 2 etc... you will get the sub-icons.
ImageList.Images.Add(Icon.FromHandle(HwndICO))
oItem.ImageIndex = oItem.Index
ElseIf fi.Attributes <> 16 Then
Debug.WriteLine("The Item @ " & oItem.Index & " is a file.")
If fi.Exists = True Then
HwndICO = ExtractIcon(Findform.Handle.ToInt32, oItem.Text, 0)
ImageList.Images.Add(Icon.FromHandle(HwndICO))
oItem.ImageIndex = oItem.Index
end if
'/// Adding in "Safley Remove Hardware" ///
If oItem.Text = "Safley Remove Hardware" Then
Debug.WriteLine("The Item @ " & oItem.Index & " is " & oItem.text)
HwndICO = ExtractIcon(Findform.Handle.ToInt32, "C:\WINDOWS\system32\hotplug.dll", 0)
ImageList.Images.Add(Icon.FromHandle(HwndICO))
oItem.ImageIndex = oItem.Index
End If
Next

Debug.Flush()
GetCurrentIcon = Nothing
End Function

You might notice there is a script that reads out "Safley Remove Hardware" in the list & gives it the proper icon from Hotplug.dll.

If you would like to execute it, check out the "How To Min & Max a Form by Process" (http://forums.dev-archive.com/showthread.php?t=145373) article I wrote.

Call it on the ListView's DoublClick Event.

Basically I am building an app for my family so they can manage their computer's better from a little simple tool interface.

Still on the TODO list & getting done after this post are adding in simple names with...

Path.GetFileNameWithoutExtension()

(Ahh lots of good stuff in the IO namespace, gotta love it.)

Some other cool perks as I come up with them, ect...

Well back on the subject there is another cool API I want to throw a link up for that does this & more... SHFILEINFO! Check out this MSDN article (http://support.microsoft.com/default.aspx?scid=kb;en-us;319340) if your interested.

Happy Programming! :D

Jugg
Jugg at 2007-11-11 21:49:38 >