扩展Ivan I?in的解决方案:
[DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
private static extern int SetWindowTheme(IntPtr hwnd, string pszSubAppName, string pszSubIdList);
public static void SetTreeViewTheme(IntPtr treeHandle) {
SetWindowTheme(treeHandle, "explorer", null);
}
Run Code Online (Sandbox Code Playgroud)
要使用,请TreeView在您的表单中添加,并在中Form_Load:
SetTreeViewTheme( treeView1.Handle );
Run Code Online (Sandbox Code Playgroud)
或者,您可以扩展TreeView对象
public class MyTreeView : TreeView
{
[DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
private static extern int SetWindowTheme(IntPtr hwnd, string pszSubAppName, string pszSubIdList);
public MyTreeView() {
SetWindowTheme(this.Handle, "explorer", null);
}
}
Run Code Online (Sandbox Code Playgroud)
我能想到的方法有3种:
阳光明媚已经提到使用SetWindowTheme(TreeView.Handle, "explorer", null)
如果可以的话使用 WPF 并添加 TreeViewItem 对象
重写OnPaint方法,这太复杂了,考虑到你只能做1个,所以1个或2个由你选择。