如何获得图标以及如何将其与GetIcon方法一起使用?

时间:2019-07-18 21:47:42

标签: c# winforms

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

public sealed class AdvancedTreeView : TreeView
{
    public AdvancedTreeView()
    {
        DrawMode = TreeViewDrawMode.OwnerDrawText;
        ShowLines = false;
        AlternateBackColor = BackColor;
    }

    public Color AlternateBackColor { 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); // 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)
    {

    }

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

        return Nodes.IndexOf(node);
    }
}

在此行:

 Image icon = GetIcon(e.Node.IsExpanded);

不确定在GetIcon方法中该怎么做。 我的文件夹中的硬盘上已经有要用作文件.ico的图标。

我想创建自己的自定义树状视图。

目标是创建这种树视图样式:

Treeview

我正在尝试使用FillPolygon,但它没有像我想要的那样创建右箭头三角形。以及如何在我的代码中使用它?

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, 10);
        Point point2 = new Point(50, 100);
        Point point3 = new Point(100, 0);

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

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

结果是:

Triangle

我还不确定三角形到底在视觉上如何协调?

例如,如果第一个点是0,50,而我想在树状视图图标屏幕快照中创建一个三角形?其余两点坐标应该是什么?

0 个答案:

没有答案