4

如何更改在ShowPlusMinus和/或ShowRootLinesare时出现的加号 (+) 和减号 (-) 图像的展开/折叠图像true

为了帮助可视化,我想制作以下 TreeView 树视图加/减 +/-

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

树视图箭头

4

3 回答 3

9

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

要使用,请将 a 添加TreeView到您的表单中,然后在Form_Load

SetTreeViewTheme( treeView1.Handle );

或者,您可以扩展 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);
    }
}

树视图之前和之后 描述调用前后的样子SetWindowTheme

于 2016-08-08T02:11:34.297 回答
4

当你想自定义你的treeview控件时,微软在treeview控件上提供了一个名为“TreeViewDrawMode”的属性,它的值是一个枚举,它有3个值:Normal、OwnerDrawText、OwnerDrawAll,在你的情况下,你必须使用OwnerDrawAll。将该属性设置为 OwnerDrawAll 后,当显示树视图的节点时,将触发名为“DrawNode”的事件,因此您可以在那里处理您的绘图。当你自己画的时候,通常你需要画3个东西:展开/折叠图标、节点图标、节点文本。我的示例如下: //定义图标文件路径 string minusPath = Application.StartupPath + Path.DirectorySeparatorChar + "minus.png"; string plusPath = Application.StartupPath + Path.DirectorySeparatorChar + "plus.png"; 字符串节点路径 = 应用程序。

    public FrmTreeView()
    {
        InitializeComponent();
        //setting to customer draw
        this.treeView1.DrawMode = TreeViewDrawMode.OwnerDrawAll;
        this.treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode);
    }

    void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
    {
        Rectangle nodeRect = e.Node.Bounds;
        /*--------- 1. draw expand/collapse icon ---------*/
        Point ptExpand = new Point(nodeRect.Location.X - 20, nodeRect.Location.Y + 2);
        Image expandImg = null;
        if (e.Node.IsExpanded || e.Node.Nodes.Count < 1)
            expandImg = Image.FromFile(minusPath);
        else
            expandImg = Image.FromFile(plusPath);
        Graphics g = Graphics.FromImage(expandImg);
        IntPtr imgPtr = g.GetHdc();
        g.ReleaseHdc();
        e.Graphics.DrawImage(expandImg, ptExpand);

        /*--------- 2. draw node icon ---------*/
        Point ptNodeIcon = new Point(nodeRect.Location.X - 4, nodeRect.Location.Y + 2);
        Image nodeImg = Image.FromFile(nodePath);
        g = Graphics.FromImage(nodeImg);
        imgPtr = g.GetHdc();
        g.ReleaseHdc();
        e.Graphics.DrawImage(nodeImg, ptNodeIcon);
        /*--------- 3. draw node text ---------*/
        Font nodeFont = e.Node.NodeFont;
        if (nodeFont == null)
            nodeFont = ((TreeView)sender).Font;
        Brush textBrush = SystemBrushes.WindowText;
        //to highlight the text when selected
        if ((e.State & TreeNodeStates.Focused) != 0)
            textBrush = SystemBrushes.HotTrack;
        //Inflate to not be cut
        Rectangle textRect = nodeRect;
        //need to extend node rect
        textRect.Width += 40;
        e.Graphics.DrawString(e.Node.Text, nodeFont, textBrush, Rectangle.Inflate(textRect, -12, 0));
     }

我的测试结果是这样的: 结果图片

于 2013-12-08T12:17:34.477 回答
4

我能想到的方法有3种:

  1. Sunny已经提到使用SetWindowTheme(TreeView.Handle, "explorer", null)

  2. 如果可以选择使用 WPF 并添加 TreeViewItem 对象

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

于 2012-11-29T12:41:17.853 回答