更改笔的箭头端盖的方向

时间:2017-01-31 12:51:23

标签: c# .net winforms

我想绘制一条直线并在该行的“尾部”显示箭头,如下所示:

enter image description here

这是我尝试过的。箭头盖放在线的末端,但其方向不正确。当我想要它向上时,它会向下移动,如上图所示。

AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);
Pen p = new Pen(Color.Black, 2);
p.CustomEndCap = bigArrow;
g.DrawLine(p, X, Y, X, 50);

2 个答案:

答案 0 :(得分:1)

这是一种解决方法,使用一个像素的额外线,但我认为将完成这项工作:

var img = new System.Drawing.Bitmap(200, 200); 
using (var g = System.Drawing.Graphics.FromImage(img)) {
   using (var p1 = new System.Drawing.Pen(System.Drawing.Color.Black, 2),
              p2 = new System.Drawing.Pen(System.Drawing.Color.Black, 2)) {
       using (var bigArrow = new AdjustableArrowCap(5, 5)) {
           p2.CustomEndCap = bigArrow;
           g.DrawLine(p1, 25, 50, 25, 100);
           g.DrawLine(p2, 25, 100, 25, 99);
        }
    }
}

这是我的输出:

enter image description here

答案 1 :(得分:0)

这个使用CustomLineCap,它不完美,但希望能给你一个好方向

Graphics g= e.Graphics;
using (var path = new GraphicsPath()) {
    path.AddLine(new Point(-4, 4), new Point(4, 4));
    path.AddLine(new Point(4, 4), new Point(0, -1));
    path.AddLine(new Point(0, -1), new Point(-4, 4));
    using (var cap = new CustomLineCap(path, null))
    using (var customCapPen = new Pen(Color.Black, 2) {CustomEndCap = cap}) { g.DrawLine(customCapPen, 10, 10, 10, 50); }
}