在 .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/