C# 用鼠标事件绘制曲线和多边形

发布于 2024-10-07 09:54:39 字数 112 浏览 0 评论 0原文

我是 C# 的初学者,所以要温柔。

在 C# 中,我想像在 Paint 中一样绘制曲线和多边形;按住鼠标左键进行绘制的位置。您能提供一些关于如何做到这一点的建议或代码吗?

谢谢。

I am a beginner in C#, so be gentle.

In C#, I want draw curves and polygons like in Paint; where you hold the left mouse button to draw. Can you give some advice or code on how to do that?

Thank you.

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

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

发布评论

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

评论(3

长安忆 2024-10-14 09:54:39

首先,尝试仅使用 Graphics.LineTo() 跟踪鼠标 - 然后开始使用鼠标向下和向上事件,然后从那里开始。图形领域有很多值得探索的地方,而且应该很有趣!

至于数组:

List<Point> points=new List<Point>();

稍后,在鼠标移动时:

points.Add(new Point(mouse.X,mouse.Y));

以及更晚的时候,如果您需要真实的点数组

Point[] pa=points.ToArray();

For the beginning, try just tracing the mouse with Graphics.LineTo() - then start to play with mouse down and up events, then go from there. There is much to explore in the graphics area, and it should be lots of fun!

As for the array:

List<Point> points=new List<Point>();

later, on mouse move:

points.Add(new Point(mouse.X,mouse.Y));

and much later, if you need real point array

Point[] pa=points.ToArray();
傲性难收 2024-10-14 09:54:39

您需要处理鼠标按下、移动和鼠标松开事件,同时保留一些数据,然后在您要绘制的任何控件的 Paint 事件上绘制这些数据。

请查看这篇 CodeProject 文章,了解您需要执行的操作的一个很好的示例。

要将点与曲线连接,您应该查看这篇关于绘制贝塞尔曲线的文章一组点。这是另一个进行样条插值

You need to handle the mouse down, move and mouse up events while persisting some data which is then drawn on the Paint event of whatever control you are painting into.

Take a look at this CodeProject article for a good example of what you need to do.

For connecting points with a curve you should look at this article for drawing Bezier curves from a set of points. Here's another one that does spline interpolation.

傲影 2024-10-14 09:54:39

Paul Sasik 引用的 CodeProject 文章是一个很好的起点。要绘制曲线,您可能需要尝试使用 Graphics.DrawBezier(...),它采用点数组作为参数,并通过点渲染曲线。不过,这会带来一些复杂性问题,因为您需要决定每个段使用多少个连续点,以及如何处理运行重叠。

The CodeProject article referenced by Paul Sasik is an excellent starting point. For drawing curved lines, you might want to try using Graphics.DrawBezier(...), which takes an array of points as a parameter, and renders a curved line through the points. This will have some complexity issues, though, as you need to decide how many consecutive points to use for each segment, and how to handle the running overlaps.

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