In this tutorial, we’ll learn How To Make a Simple Program Like MS Paint .
Open Visual Studio 2015 and click on File –> New –> Project which opens up the template window with many developing options.The above step brings in an empty Windows Form where the user can use Tools for designing the empty form into interactive form.
We Will use some events those are MouseMove, mouseDown and mouseUp.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace simple_paint { public partial class Form1 : Form { public Form1() { InitializeComponent(); } bool drw; int beginX, beginY; private void Form1_MouseUp(object sender, MouseEventArgs e) { drw = false; } private void Form1_MouseDown(object sender, MouseEventArgs e) { drw = true; beginX = e.X; beginY = e.Y; } private void Form1_MouseMove(object sender, MouseEventArgs e) { Graphics g = this.CreateGraphics(); Pen p = new Pen(Color.White, 4); Point point1 = new Point(beginX, beginY); Point point2 = new Point(e.X, e.Y); if (drw == true) { g.DrawLine(p, point1, point2); beginX = e.X; beginY = e.Y; } } private void Form1_Load(object sender, EventArgs e) { this.Text = "csharp-console-examples.com"; this.BackColor = Color.Black; } } } |
Output: