如何更改由+组成的我的treeView图标,就像c#.net win窗体中的Windows资源管理器treeview

use*_*718 3 c# treeview uxtheme winforms

( - )出现时,图像如何从加号(+)和减号改变展开/折叠的图像ShowPlusMinus和/或ShowRootLinestrue

为了使可视化,我想制作以下TreeView 树状视图正负+/-

看起来像这样(例如Windows资源管理器)

树状箭头

Kra*_*ime 5

扩展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)

Treeview之前和之后 描述通话前后的样子 SetWindowTheme

  • 在控件构造函数中调用SetWindowTheme可能存在问题-您可能还没有句柄。更为可靠的方法是重写OnHandleCreated事件并在那里进行。旧的操作系统也有可能对此进行呕吐(预览),因此检查OS版本是否合适-如下所示(不是我的工作,而是我遵循的使工作方式):https ://www.cyotek.com/blog/enabling-shell-styles-for-the-listview-and-treeview-controls-in-csharp (2认同)

Iva*_*čin 4

我能想到的方法有3种:

  1. 阳光明媚已经提到使用SetWindowTheme(TreeView.Handle, "explorer", null)

  2. 如果可以的话使用 WPF 并添加 TreeViewItem 对象

  3. 重写OnPaint方法,这太复杂了,考虑到你只能做1个,所以1个或2个由你选择。