c# - 在 C# 中绘制带有尖锐终点的​​箭头

标签 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);

                }


            }

最佳答案

我已经稍微修改了你的代码以使其更容易,但我想你可以根据你的目的修改它:

    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();
    }

我已经为节点定义了实体类如下:

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

并定义 CustomEndCap 而不是 EndCap:

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

关于c# - 在 C# 中绘制带有尖锐终点的​​箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27032935/

相关文章:

c# - 在 C# 中实现 SNMP Reporter(代理)(必须在 Mono 上运行)?

c# - 将 JS 转换为 C#

c# - 无法获得在 VS2010 中使用 IISOle 对象的引用

c# - DataGridView 图像未显示在未绑定(bind)列中

c# - 如何创建一个 Button,它可以在不窃取焦点的情况下将键发送到控件 - Virtual Keyboard

c# - 使用参数启动进程

android - 有什么方法可以通过双击自动访问 Logcat 中的任何登录?

c++ - 检查线是否穿过二维 map 上的墙

c# - 使用 ItextSharp 复制没有松散表单字段结构的 PDF

javascript - JavaScript中直线和圆之间的碰撞检测