如何让Windows本机查找.NET TreeView?

asm*_*smo 28 .net windows treeview winforms

树

在.NET中使用TreeView组件时,我会看到左侧树的外观.如何为我的.NET TreeView获取正确树(Windows Native Look)的外观?

我特别想要的是"三角形"节点手柄和蓝色"气泡"选择方块.

Dav*_*nan 45

您需要P/Invoke调用SetWindowTheme传递树的窗口句柄并使用"explorer"作为主题.

将以下代码粘贴到项目中的新类中,编译并使用此自定义控件而不是内置TreeView控件.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

请注意,此技巧对ListView控件的工作方式也完全相同.

  • @Unknown 树视图控件的哪一部分对您来说是黑色的? (2认同)