如何在 Windows 窗体中制作 Triangle 对象?

发布于 01-16 19:33 字数 340 浏览 3 评论 0原文

我想绘制/制作一个三角形图形,该图形将在运行程序后立即出现,但我无法找出正确的命令。这是我用来制作矩形对象的命令。

private void Form1_Paint(object sender, PaintEventArgs e)
{ 
e.Graphics.FillRectangle(Brushes.Aquamarine, _x, _y, 100, 100); 
} 

Example

因此,当我制作对象时,我将使其自动移动。

我搜索过教程但找不到任何合适的。请帮忙。

I want to draw/make a triangle graphic that will appear right after when I run the program but I can't figure out the right command. Here's the command I use to make a rectangle object.

private void Form1_Paint(object sender, PaintEventArgs e)
{ 
e.Graphics.FillRectangle(Brushes.Aquamarine, _x, _y, 100, 100); 
} 

Example

So when I make the object, I'll make it move automatically.

I've searched for tutorials but couldn't find anything suitable. Please help.

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

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

发布评论

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

评论(2

潇烟暮雨2025-01-23 19:33:14

您可以使用FillPolygon并指定3个三角形点。

e.Graphics.FillPolygon(Brushes.Aquamarine, new Point[] { new Point(150, 100), new Point(100, 200), new Point(200, 200) });

或者您可以创建一个 FillTriangle 扩展方法

public static class Extensions
{
    public static void FillTriangle(this Graphics g, PaintEventArgs e, Point p, int size)
    {
        e.Graphics.FillPolygon(Brushes.Aquamarine, new Point[] { p, new Point(p.X - size, p.Y + (int)(size * Math.Sqrt(3))), new Point(p.X + size, p.Y + (int)(size * Math.Sqrt(3))) });
    }
}

并像这样调用

e.Graphics.FillTriangle(e, new Point(150, 100), 500);

对于直角三角形使用此

e.Graphics.FillPolygon(Brushes.Aquamarine, new Point[] { p, new Point(p.X, p.Y + size * 2), new Point(p.X + size, p.Y + size * 2) });

对于钝角此

e.Graphics.FillPolygon(Brushes.Aquamarine, new Point[] { p, new Point(p.X - size, p.Y + height), new Point(p.X + size, p.Y + height) });

此代码将输出此

e.Graphics.FillRightTriangle(e, new Point(50, 20), 100);
e.Graphics.FillTriangle(e, new Point(400, 20), 70);
e.Graphics.FillObtuseTriangle(e, new Point(230, 200), 50, 130);

在此处输入图像描述

You can use FillPolygon and specify 3 triangle points.

e.Graphics.FillPolygon(Brushes.Aquamarine, new Point[] { new Point(150, 100), new Point(100, 200), new Point(200, 200) });

Or you can create a FillTriangle extension method

public static class Extensions
{
    public static void FillTriangle(this Graphics g, PaintEventArgs e, Point p, int size)
    {
        e.Graphics.FillPolygon(Brushes.Aquamarine, new Point[] { p, new Point(p.X - size, p.Y + (int)(size * Math.Sqrt(3))), new Point(p.X + size, p.Y + (int)(size * Math.Sqrt(3))) });
    }
}

And call like this

e.Graphics.FillTriangle(e, new Point(150, 100), 500);

For right triangle use this

e.Graphics.FillPolygon(Brushes.Aquamarine, new Point[] { p, new Point(p.X, p.Y + size * 2), new Point(p.X + size, p.Y + size * 2) });

For obtuse this

e.Graphics.FillPolygon(Brushes.Aquamarine, new Point[] { p, new Point(p.X - size, p.Y + height), new Point(p.X + size, p.Y + height) });

This code will output this

e.Graphics.FillRightTriangle(e, new Point(50, 20), 100);
e.Graphics.FillTriangle(e, new Point(400, 20), 70);
e.Graphics.FillObtuseTriangle(e, new Point(230, 200), 50, 130);

enter image description here

不及他2025-01-23 19:33:14

DrawPolygon 可能可以满足您的要求,您只需要指定三角形的三个顶点即可。

https: //learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.drawpolygon?view=dotnet-plat-ext-6.0

编辑: FillPolygon 可能更符合您的需要,抱歉。

https: //learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.fillpolygon?view=dotnet-plat-ext-6.0

There is DrawPolygon which probably does what you want, you just need to specify the three vertices of your triangle.

https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.drawpolygon?view=dotnet-plat-ext-6.0

Edit: FillPolygon is probably more in line with what you need, sorry.

https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.fillpolygon?view=dotnet-plat-ext-6.0

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