Dav*_*nan 45
您需要P/Invoke调用SetWindowTheme传递树的窗口句柄并使用"explorer"作为主题.
将以下代码粘贴到项目中的新类中,编译并使用此自定义控件而不是内置TreeView控件.
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)
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控件的工作方式也完全相同.