为什么代码永远无法到达重写OnDrawNode方法?

时间:2019-07-19 08:12:53

标签: c# winforms

在form1设计器中,我添加了一个treeview控件,并向其中添加了一个根节点和一个子节点。并创建了绘画事件。

在form1代码中:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            AdvancedTreeView atv = new AdvancedTreeView();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            AdvancedTreeView.FillPolygonPoint(e);
        }
    }
}

AdvancedTreeView类:

using System;
using System.Drawing;
using System.Windows.Forms;

public class AdvancedTreeView : TreeView
{
    private static Image myimage;

    public AdvancedTreeView()
    {
        DrawMode = TreeViewDrawMode.OwnerDrawAll;
        ShowLines = false;
        AlternateBackColor = BackColor;
    }

    public Color AlternateBackColor { get; set; }

    protected override void OnDrawNode(DrawTreeNodeEventArgs e)
    {
        e.DrawDefault = true;
        base.OnDrawNode(e);

        // background
        Color backColor = (GetTopNodeIndex(e.Node) & 1) == 0 ? BackColor : AlternateBackColor;
        using (Brush b = new SolidBrush(backColor))
        {
            e.Graphics.FillRectangle(b, new Rectangle(0, e.Bounds.Top, ClientSize.Width, e.Bounds.Height));
        }

        // icon
        if (e.Node.Nodes.Count > 0)
        {

            Image icon = GetIcon(e.Node.IsExpanded); // TODO: true=down;false:right
            e.Graphics.DrawImage(icon, e.Bounds.Left - icon.Width - 3, e.Bounds.Top);
        }

        // text (due to OwnerDrawText mode, indenting of e.Bounds will be correct)
        TextRenderer.DrawText(e.Graphics, e.Node.Text, Font, e.Bounds, ForeColor);

        // indicate selection (if not by backColor):
        if ((e.State & TreeNodeStates.Selected) != 0)
            ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds);
    }

    private Image GetIcon(bool isExpanded)
    {
        return myimage;
    }

    private int GetTopNodeIndex(TreeNode node)
    {
        while (node.Parent != null)
            node = node.Parent;

        return Nodes.IndexOf(node);
    }

    public static void FillPolygonPoint(PaintEventArgs e)
    {

        // Create solid brush.
        SolidBrush blueBrush = new SolidBrush(Color.Blue);

        // Create points that define polygon.
        Point point1 = new Point(0, 0);
        Point point2 = new Point(20, 10);
        Point point3 = new Point(0, 20);

        Point[] curvePoints = { point1 , point2, point3 };

        // Draw polygon to screen.
        e.Graphics.FillPolygon(blueBrush, curvePoints);

        myimage = new Bitmap(10,10,e.Graphics);
    }
}

它进入AdvancedTreeView中的一行:

DrawMode = TreeViewDrawMode.OwnerDrawAll;

但是它永远不会进入OnDrawNode。 我也尝试过TreeViewDrawMode.OwnerDrawText,但是它永远都不会到达OnDrawNode。

1 个答案:

答案 0 :(得分:1)

因此,这是来自旧答案的“原始” AdvancedTreeView示例的完整版本。

新成员是openedIconclosedIcon字段,ArrowColor属性和GetIcon / InitIcon方法。

public class AdvancedTreeView : TreeView
{
    private Bitmap openedIcon, closedIcon;

    public AdvancedTreeView()
    {
        DrawMode = TreeViewDrawMode.OwnerDrawText;
        ShowLines = false;
        AlternateBackColor = BackColor;
        ArrowColor = SystemColors.WindowText;
    }

    public Color AlternateBackColor { get; set; }
    public Color ArrowColor { get; set; }

    protected override void OnDrawNode(DrawTreeNodeEventArgs e)
    {
        // background
        Color backColor = (GetTopNodeIndex(e.Node) & 1) == 0 ? BackColor : AlternateBackColor;
        using (Brush b = new SolidBrush(backColor))
        {
            e.Graphics.FillRectangle(b, new Rectangle(0, e.Bounds.Top, ClientSize.Width, e.Bounds.Height));
        }

        // icon
        if (e.Node.Nodes.Count > 0)
        {
            Image icon = GetIcon(e.Node.IsExpanded);
            e.Graphics.DrawImage(icon, e.Bounds.Left - icon.Width - 3, e.Bounds.Top);
        }

        // text (due to OwnerDrawText mode, indenting of e.Bounds will be correct)
        TextRenderer.DrawText(e.Graphics, e.Node.Text, Font, e.Bounds, ForeColor);

        // indicate selection (if not by backColor):
        if ((e.State & TreeNodeStates.Selected) != 0)
            ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds);
    }

    private int GetTopNodeIndex(TreeNode node)
    {
        while (node.Parent != null)
            node = node.Parent;

        return Nodes.IndexOf(node);
    }

    private Image GetIcon(bool nodeIsExpanded)
    {
        if (openedIcon == null)
            InitIcons();
        return nodeIsExpanded ? openedIcon : closedIcon;
    }

    private void InitIcons()
    {
        openedIcon = new Bitmap(16, 16);
        closedIcon = new Bitmap(16, 16);
        using (Brush b = new SolidBrush(ArrowColor))
        {
            using (Graphics g = Graphics.FromImage(openedIcon))
                g.FillPolygon(b, new[] { new Point(0, 0), new Point(15, 0), new Point(8, 15), });
            using (Graphics g = Graphics.FromImage(closedIcon))
                g.FillPolygon(b, new[] { new Point(0, 0), new Point(15, 8), new Point(0, 15), });
        }
    }
}

设置颜色并添加节点后的设计师:

enter image description here

在运行时:

enter image description here

仍然可以通过更改颜色时使颜色无效等来改进...