/* * A C# program that responds to mouse events with drawing. * * Ben Bederson, February 20, 2002 */ using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; public class Draw : System.Windows.Forms.Form { static public void Main() { Application.Run(new Draw()); } public Draw() { Size = new Size(400, 400); StartPosition = FormStartPosition.CenterScreen; DrawingCanvas control = new DrawingCanvas(); control.Location = new Point(0, 0); control.Size = new Size(400, 400); Controls.Add(control); } } class DrawingCanvas : Control { Pen blackPen = new Pen(Color.Black); GraphicsPath path = null; int prevx, prevy; public DrawingCanvas() { BackColor = Color.White; MouseDown += new MouseEventHandler(mouseDownHandler); MouseMove += new MouseEventHandler(mouseMoveHandler); } public void mouseDownHandler(object sender, MouseEventArgs e) { prevx = e.X; prevy = e.Y; path = new GraphicsPath(); } public void mouseMoveHandler(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.None) { path.AddLine(prevx, prevy, e.X, e.Y); prevx = e.X; prevy = e.Y; Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; if (path != null) { g.DrawPath(blackPen, path); } } }