Search code examples
c#.netwinformsgraphicspaintevent

How to use the Paint event to draw shapes at mouse coordinates


I recently started programming in C# obviously and was trying to do a simple WinForms app that takes mouse coordinates and scales a Rectangle according to the coordinates.

The issue I am facing is that I don't know how to call a method that uses more arguments (in this case is x, y and PaintEventArgs). Or, I do know what to do with the PaintEvent.

Here is the whole code, since its pretty short and rather simple:

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class Form1 : Form
{
    public void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        int x = e.X; 
        int y = e.Y;
        String data = (x.ToString() + " " + y.ToString());
        DrawRect(Something, x, y);
    }

    PaintEventArgs pEventArgs;
    private void Form1_Paint(object sender, PaintEventArgs e)
    {

    }

    public void DrawRect(PaintEventArgs e, int rey, int rex)
    {
        Graphics gr = e.Graphics;
        Pen pen = new Pen(Color.Azure, 4);
        Rectangle rect = new Rectangle(0, 0, rex, rey);
        gr.DrawRectangle(pen, rect);
    }
}

I'm trying to call the DrawRect() method to draw the Rectangle with width and height according to the mouse coordinates.

So how can I call the DrawRect() with coordinates and PaintEventArgs?


Solution

  • The PaintEventArgs allows you to access to the Graphics object, you need that one to draw something.

    If you don't want to use the PaintEventArgs, i suggest that you call the CreateGraphics() method of your Form, and it will allow you to draw the rectangle.

    To improve performance, i suggest that you use the using(...){ } keywork in order to dispose the Graphics object and the Pen object.

    You need to include System.Drawing in order to use Graphics and Pen.

    You're code will look like that :

    using System.Drawing;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp2
    {
        public partial class Form1 : Form
        {
            Point _coordinates;
    
            public Form1()
            {
                this._coordinates = new Point();
                this.InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
            }
    
            public void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                this._coordinates = new Point(e.X, e.Y);
                this.Invalidate();
            }
    
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                // Don't draw on first Paint event
                if(this._coordinates.X != 0 && this._coordinates.Y != 0)
                {
                    this.DrawRect(e);
                }
            }
    
            public void DrawRect(PaintEventArgs e)
            {
                using (Pen pen = new Pen(Color.Azure, 4))
                {
                    Rectangle rect = new Rectangle(0, 0, this._coordinates.X, this._coordinates.Y);
                    e.Graphics.DrawRectangle(pen, rect);
                }
            }
        }
    }