Creating a Paint Program when Moving Mouse using C#

In this tutorial, i will teach you how to create a program that paints the form when clicking and moving the mouse using C#. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your program as Mouse Move Paint. 2. You don't have to design your interface because we will only focus on the mouse event. 3. Now put this code for your code module.
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9. namespace Mouse_Move_Paint
  10. {
  11.         public partial class Form1
  12.         {
  13.                 public Form1()
  14.                 {
  15.                         InitializeComponent();
  16.                        
  17.                 }
  18.                
  19.  
  20.                 public void Form1_Load(System.Object sender, System.EventArgs e)
  21.                 {
  22.                         // make your cursor as a hand cursor when painting in the form
  23.                         Cursor = Cursors.Hand;
  24.                 }
  25.                 // create a boolean variable named mustPaint and has a False value in the first run of the program
  26.                 bool mustPaint = false;
  27.                
  28.                 public void MouseEvent_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  29.                 {
  30.                 //make mustPaint variable to True when
  31.                         mustPaint = true;
  32.                 }
  33.                
  34.                 public void MouseEvent_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  35.                 {
  36.                 //This code is for moving the mouse, when making the mustPaint variable into True
  37.                  
  38.                         if (mustPaint)
  39.                         {
  40.                         // This Graphics class delivers methods for drawing objects to the display form.
  41.                                 Graphics graphic = CreateGraphics();
  42.                         // The graphics variable has the FillEllipse Method which fills the inner of an ellipse defined by a rectangle
  43.                         // specified by a pair of coordinates (x and y in our program), a width (that is 10), and a height (5).
  44.                         // The SolidBrush Class defines a brush of a single color in which we use Green color.
  45.                                 graphic.FillEllipse(new SolidBrush(Color.Green), e.X, e.Y, 10, 5);
  46.                         }
  47.                 }
  48.                
  49.                 public void MouseEvent_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  50.                 {
  51.                         //make the mustPaint variable into false
  52.                         mustPaint = false;
  53.                 }
  54.         }
  55. }
Output: output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment