Drawing a line using mouse during runtime in C#

Drawing a line in design time is simple. We just need is to call DrawLine() method.

To draw a line during run time we can use the mouse. So we need several things to draw a line, a Pen, a starting Point, and an end Point.
During design time it is very easy to draw a line by just specifying the above things in the code.
Lets start it...
Use the below code in your code editor.


using System.Drawing;


Create two Points p1 and p2 in the Class.


Point p1 = new Point();
Point p2 = new Point();


Obtaining the starting point
-----------------------------------

First we need is a Point in which the line starts ie, the starting point
The location in the form where the left mouse button is pressed is the starting point of the line. So we have to write the code in the MouseDown event of the Form.
The code will look like


private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button==MouseButtons.Left)
            p1 = e.Location;
        }


Here p1 is assigned a value equal to the location of the mouse in which the left mouse button is pressed.
Ie, p1 is the point where the left mouse button is pressed.

Obtaining the ending point and drawing line
---------------------------------------------------------

The end point will be the point where the left mouse button is released.
Ie, we have to write code in the MouseUp event of the Form.
p2 is assigned a value equal to the location of the mouse, ie, point where left mouse button is released.
Create a pen like


Pen pen = new Pen(Color.Blue, 1); 


You can use any color and any width of type float.
Create the graphics by CreateGraphics() method and then draw the line by using DrawLine() method.
The code is given below


private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            Pen pen = new Pen(Color.Blue, 1);             
            if (e.Button == MouseButtons.Left)
            {
                p2 = e.Location;
                Graphics g = this.CreateGraphics();
                g.DrawLine(pen,p1, p2);
               
            }
        }



The overall code is given below


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Point p1 = new Point();
        Point p2 = new Point();
  
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button==MouseButtons.Left)
            p1 = e.Location;
        }
      
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            Pen pen = new Pen(Color.Blue, 1);
            if (e.Button == MouseButtons.Left)
            {
                p2 = e.Location;
                Graphics g = this.CreateGraphics();
                g.DrawLine(pen, p1, p2);
            }
        }
    }
}



Run the program by pressing F5.
Draw lines by dragging the mouse in the Form.

Comments