.net - 如何获得 .NET TreeView 的 Windows 原生外观?

标签 .net windows winforms treeview

Trees

在 .NET 中使用 TreeView 组件时,我得到了左侧树的外观。 如何为我的 .NET TreeView 获得正确的树外观(Windows native 外观)?

我特别想得到的是“三角形”节点 handle 和蓝色“气泡”选择方 block 。

最佳答案

你需要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);
    }
}

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

请注意,此技巧也适用于 ListView 控件。

关于.net - 如何获得 .NET TreeView 的 Windows 原生外观?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5131534/

相关文章:

.net - 从数据库查询填充组合框

c# - 优化多余的else语句

ruby-on-rails - 我应该如何将 ID 传递到收集 route ?

c# - 发送回数据库时将换行符替换为\r\n

c# - 在 winforms 中使用 c# 在选中列表框中的项目索引

.net - Windows 上 native 加密哈希的 native 实现比 .Net 托管版本快多少?

windows - 在 XP 中如何查看哪些进程/程序正在通过互联网发送/接收数据?

windows - bat - ECHO 在 txt 文件中关闭

c# - 将 CheckedListBox 项目保存到设置

.net - Azure DevOps - 无法运行已安装的 dotnet 工具