Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following code, that draws a line with a (very) small arrow...

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

I want to draw a big arrow (circle, square, triangle etc...), keeping the same line width.

Is it possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
457 views
Welcome To Ask or Share your Answers For Others

1 Answer

You'd want to use a CustomLineCap with a GraphicsPath. Here's an example:

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

You want to "design" your cap with a line going top-to-bottom and from (0, 0) to get the correct coordinates.

EDIT: I just wanted to mention that you can also use AdjustableArrowCap to draw an arrow of a specific size and fill it but because you mentioned the requirement for other shapes, I've used a CustomLineCap.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...