在C#中使用尖端点绘制箭头

时间:2014-11-20 06:11:31

标签: c# winforms line

我要在我的winform画一些箭,我完成了大部分工作。只有一个问题,我无法区分箭头的结束和起点,它们相互混合,它是不清楚每个箭头的起点在哪里以及它的终点在哪里,我附上了一个显示我的问题的图像。我想知道如何让它们更清晰或者像那样(用户友好的可能)箭头很容易看到箭头。 enter image description here 正如你所看到的那样,因为箭头相互跟随,它们的结束和开始是混合的,我该如何解决这个问题呢? 谢谢您的帮助 这是我的代码

      using (Pen P = new Pen(Color.LightBlue, 3))
            {

                P.StartCap = LineCap.NoAnchor;
                P.EndCap = LineCap.ArrowAnchor;

                for (int i = 0; i < result.Length; i++)
                {
                    int a = (int)result[i] - 65;
                    int b;
                    try
                    {
                        b = (int)result[i + 1] - 65;
                    }
                    catch (Exception)
                    {

                        b = (int)result[0] - 65;
                    }

                   Point startPoint = new Point(_guiLocations[a].X, _guiLocations[a].Y);
                   Point endPoint = new Point(_guiLocations[b].X, _guiLocations[b].Y);
                   pnlView.CreateGraphics().DrawLine(P, startPoint, endPoint);

                }


            }

1 个答案:

答案 0 :(得分:3)

我已经修改了你的代码以使其更容易,但我想你可以修改它为你的purporses:

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        var nodes = new[] { new Node { Code = 'A', Position = new Point(10, 10) }, new Node { Code = 'B', Position = new Point(45, 45) } };

        using (Pen P = new Pen(Color.LightBlue, 3))
        {

            P.StartCap = LineCap.NoAnchor;
            P.CustomEndCap = new AdjustableArrowCap(4, 8, false);

            for (int i = 0; i < nodes.Length; i++)
            {
                var node = nodes[i];
                for (int j = i; j < nodes.Length; j++)
                {
                    var node2 = nodes[j];

                    if (node == node2)
                        continue;

                    Point startPoint = new Point(node.Position.X, node.Position.Y);
                    Point endPoint = new Point(node2.Position.X, node2.Position.Y);
                    pnlView.CreateGraphics().DrawLine(P, startPoint, endPoint);
                }
            }
        }
        pnlView.PerformLayout();
    }

我为Node定义了实体类,如下所示:

class Node
{
     public char Code { get; set; }
     public Point Position { get; set; }
}

定义CustomEndCap而不是EndCap:

P.CustomEndCap = new AdjustableArrowCap(4, 8, false);