31

Trees

When using the TreeView component in .NET, I get the look of the left tree. How can I get the look of the right tree (Windows Native Look) for my .NET TreeView?

What I especially want to get is the "triangle" node handles and the blue "bubble" selection square.

1 Answers1

46

You need to P/Invoke to call SetWindowTheme passing the window handle of the tree and use "explorer" as the theme.

Paste the following code into a new class in your project, compile, and use this custom control instead of the built-in TreeView control.

C#:

public class NativeTreeView : System.Windows.Forms.TreeView
{
    [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
    private extern static int SetWindowTheme(IntPtr hWnd, string pszSubAppName,
                                            string pszSubIdList);

    protected override void CreateHandle()
    {
        base.CreateHandle();
        SetWindowTheme(this.Handle, "explorer", null);
    }
}

VB.NET:

Public Class NativeTreeView : Inherits TreeView

    Private Declare Unicode Function SetWindowTheme Lib "uxtheme.dll"
        (hWnd As IntPtr, pszSubAppName As String, pszSubIdList As String) As Integer

    Protected Overrides Sub CreateHandle()
        MyBase.CreateHandle()
        SetWindowTheme(Me.Handle, "Explorer", Nothing)
    End Sub

End Class

Note that this trick also works exactly the same way for the ListView control.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • @cody thanks for the edit I'm on my phone today so this was from memory! – David Heffernan Feb 27 '11 at 10:52
  • Sure. The other answers were just wrong, and I didn't see the point in posting my own answer just for a code sample, when you'd already gotten the hard part. [It seems like you're on your phone a lot. You really ought to invest in a computer. Makes it much easier to write code. :-)] – Cody Gray - on strike Feb 27 '11 at 10:55
  • I've try to convert that code into vb.net and tested it, but it was still the same treeview. – illumi Jun 26 '14 at 00:51
  • @dave No, that's no problem – David Heffernan Jun 27 '14 at 06:00
  • @DavidHeffernan Illumi is correct, I confirm that in Win8 this doesn't work unless you run your program as administrator, strange why is this bihaveour? – TMS Oct 31 '14 at 18:28
  • @DavidHeffernan, yes my mistake, sorry, it really works. – TMS Nov 29 '14 at 12:32
  • IMPORTANT: When I move the cursor over the items, it not changes the style like windows explorer, is there some sendmessage or some P/Invoke or override for that –  Dec 11 '15 at 17:25
  • @Codigos All caps is considered rude. You have a new question. Please ask it. – David Heffernan Dec 11 '15 at 17:34
  • @DavidHeffernan TreeView "over" effects is not applied when I use this method, I saw Windows Explorer has a effect to move the cursor over the items (like ListView), when I call native setWindowTheme with ListView, it has its effects (hover effects), but TreeView does not have. –  Dec 11 '15 at 23:01
  • @Codigos You are mistaken in doing this in a comment to another question. Ask a question. Give lots of detail. Screenshots are great for this type of question. – David Heffernan Dec 11 '15 at 23:05
  • Thanks for this David, I've been struggling with a lot of OwnerDrawAll for TreeView to achieve simmilar look as Explorer. And then I found this lol – LuckyLuke82 Jan 17 '18 at 13:38
  • Hi, I know this is a bit late but can i ask how can i change the color of it? It's default color is black i want it to be white. – Buchiman Feb 11 '19 at 08:45
  • 2
    @Unknown Which part of the tree view control is black for you? – David Heffernan Feb 11 '19 at 08:52
  • @DavidHeffernan the icon that replaces the plus minus. ( > ^ ) – Buchiman Feb 11 '19 at 09:01
  • @DavidHeffernan Can i change it to White? How? – Buchiman Feb 11 '19 at 09:03
  • 1
    @Unknown The entire point of using the system theme is that it is consistent across all applications. If you want to change the way the control is painted you need to take over painting it yourself. – David Heffernan Feb 11 '19 at 09:04