C# draw curve and polygon with mouse events
I am a beg开发者_如何学编程inner 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.
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();
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.
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.
精彩评论