如何为 .NET TreeView 获得 Windows 本机外观? [英] How to get Windows native look for the .NET TreeView?

查看:20
本文介绍了如何为 .NET TreeView 获得 Windows 本机外观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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

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

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

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 控件的工作方式也完全相同.

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

这篇关于如何为 .NET TreeView 获得 Windows 本机外观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆