如何画一条带箭头的线?

发布于 2024-09-12 17:12:10 字数 382 浏览 7 评论 0原文

我有以下代码,它用一个(非常)小箭头绘制一条线...

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单挑你×的.吻 2024-09-19 17:12:10

您希望将 CustomLineCapGraphicsPath 一起使用。下面是一个示例:

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 绘制特定尺寸的箭头并填充它,但因为您提到了其他形状的要求,所以我'我用过 CustomLineCap。

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.

最偏执的依靠 2024-09-19 17:12:10

正如 TheCloudlessSky 提到的,AdjustableArrowCap 可用于绘制箭头。

public static void DrawLine(Graphics g, Point from, Point to)
{
    var pen = new Pen(Color.Black, 1);

    pen.CustomEndCap = new AdjustableArrowCap(5, 5);

    g.DrawLine(pen, from, to);
}

As TheCloudlessSky mentioned, AdjustableArrowCap can be used to draw the arrow head.

public static void DrawLine(Graphics g, Point from, Point to)
{
    var pen = new Pen(Color.Black, 1);

    pen.CustomEndCap = new AdjustableArrowCap(5, 5);

    g.DrawLine(pen, from, to);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文