How do I draw an ellipse with mouse down, move, and up actions in a Windows Forms application?
Locked
-
Wednesday, April 08, 2009 6:57 AM
How do I draw an ellipse with mouse down, move, and up actions in a Windows Forms application?
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
All Replies
-
Wednesday, April 08, 2009 6:57 AM
Using the following code snippet, we can draw an ellipse in a Windows Forms application, just as we do in the Windows Paint application.
public partial class Form1 : Form { // Define variables to determine a ellipse struct Ellipse { // The point user press Mouse Down public Point start; // The point user let Mouse Up public Point end; // Direction comparing the end point and the start point public Direction direction; } // Four quadrants in coordinate enum Direction { One, Two, Three, Four } // Store all the ellipses List<Ellipse> mEllipses = new List<Ellipse>(); // The current drawing ellipse Ellipse mEllipse; // Whether Mouse is Down to draw ellipse bool mDrawing; public Form1() { InitializeComponent(); } // Draw ellipse by its start, end points and direction private void DrawEllipse(Ellipse ellipse, PaintEventArgs e) { switch (ellipse.direction) { case Direction.One: e.Graphics.DrawEllipse(Pens.Black, new Rectangle(new Point(ellipse.start.X, ellipse.end.Y), new Size(ellipse.end.X - ellipse.start.X, ellipse.start.Y - ellipse.end.Y))); break; case Direction.Two: e.Graphics.DrawEllipse(Pens.Black, new Rectangle(ellipse.end, new Size(ellipse.start.X - ellipse.end.X, ellipse.start.Y - ellipse.end.Y))); break; case Direction.Three: e.Graphics.DrawEllipse(Pens.Black, new Rectangle(new Point(ellipse.end.X, ellipse.start.Y), new Size(ellipse.start.X - ellipse.end.X, ellipse.end.Y - ellipse.start.Y))); break; case Direction.Four: e.Graphics.DrawEllipse(Pens.Black, new Rectangle(ellipse.start, new Size(ellipse.end.X - ellipse.start.X, ellipse.end.Y - ellipse.start.Y))); break; default: MessageBox.Show("Error"); break; } } // Determine the direction of ellipse by start and end points private void DetermineDirection(MouseEventArgs e) { if (e.Y < 0 && e.X < 0) { mEllipse.end.X = 0; mEllipse.end.Y = 0; } else if (e.X < 0) { mEllipse.end.X = 0; mEllipse.end.Y = e.Y; } else if (e.Y < 0) { mEllipse.end.X = e.X; mEllipse.end.Y = 0; } else mEllipse.end = e.Location; if (mEllipse.end.X > mEllipse.start.X && mEllipse.end.Y <= mEllipse.start.Y) mEllipse.direction = Direction.One; else if (mEllipse.end.X <= mEllipse.start.X && mEllipse.end.Y < mEllipse.start.Y) mEllipse.direction = Direction.Two; else if (mEllipse.end.X < mEllipse.start.X && mEllipse.end.Y >= mEllipse.start.Y) mEllipse.direction = Direction.Three; else if (mEllipse.end.X >= mEllipse.start.X && mEllipse.end.Y > mEllipse.start.Y) mEllipse.direction = Direction.Four; } private void Form1_Paint(object sender, PaintEventArgs e) { foreach (Ellipse ellipse in mEllipses) this.DrawEllipse(ellipse, e); if (mDrawing) this.DrawEllipse(mEllipse, e); } private void Form1_MouseDown(object sender, MouseEventArgs e) { // Mouse Down to determine start point of an ellipse mEllipse.start = e.Location; if (e.Button == MouseButtons.Left) mDrawing = true; else this.Invalidate(); } private void Form1_MouseUp(object sender, MouseEventArgs e) { if (!mDrawing) return; mDrawing = false; this.DetermineDirection(e); // Add the newly created ellipse into ellipse list mEllipses.Add(mEllipse); this.Invalidate(); } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (!mDrawing) return; // When Mouse Move, determine the direction of an ellipse // constantly this.DetermineDirection(e); this.Invalidate(); } }
For more FAQ about Visual C# General, please see Visual C# General FAQ
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer by Xiaoyun Li – MSFT Wednesday, April 08, 2009 7:01 AM
- Marked As Answer by Xiaoyun Li – MSFT Wednesday, April 08, 2009 7:54 AM

