代码之家  ›  专栏  ›  技术社区  ›  serhio

如何:用箭头画一条线?

  •  10
  • serhio  · 技术社区  · 15 年前

    我有下面的代码,用一个(非常)小的箭头画一条线。。。

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Pen p = new Pen(Color.Black);
            p.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
    
            e.Graphics.DrawLine(p, 10, 10, 100, 100);
            p.Dispose();
        }
    

    大的 箭头( 圆、正方形、三角形 等等……), 保持相同的线宽 .

    有可能吗?

    1 回复  |  直到 11 年前
        1
  •  13
  •   TheCloudlessSky    15 年前

    你应该用 CustomLineCap GraphicsPath

    using(Pen p = new Pen(Color.Black))
    using(GraphicsPath capPath = new GraphicsPath())
    {
        // A triangle
        capPath.AddLine(-20, 0, 20, 0);
        capPath.AddLine(-20, 0, 0, 20);
        capPath.AddLine(0, 20, 20, 0);
    
        p.CustomEndCap = new System.Drawing.Drawing2D.CustomLineCap(null, capPath);
    
        e.Graphics.DrawLine(p, 0, 50, 100, 50);
    }
    

    你想“设计”你的帽子与一条线从上到下,从(0,0)得到正确的坐标。

    编辑 :我只想提一下,您也可以使用 AdjustableArrowCap

    推荐文章