开发者

How to get Windows native look for the .NET TreeView?

开发者 https://www.devze.com 2023-02-13 21:47 出处:网络
When using the TreeView component in .NET, I get the look of the left tree. How can I get the look of t开发者_开发知识库he right tree (Windows Native Look) for my .NET TreeView?

How to get Windows native look for the .NET TreeView?

When using the TreeView component in .NET, I get the look of the left tree. How can I get the look of t开发者_开发知识库he 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消