C# 用鼠标事件绘制曲线和多边形
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,尝试仅使用 Graphics.LineTo() 跟踪鼠标 - 然后开始使用鼠标向下和向上事件,然后从那里开始。图形领域有很多值得探索的地方,而且应该很有趣!
至于数组:
稍后,在鼠标移动时:
以及更晚的时候,如果您需要真实的点数组
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:
later, on mouse move:
and much later, if you need real point array
您需要处理鼠标按下、移动和鼠标松开事件,同时保留一些数据,然后在您要绘制的任何控件的 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.
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.