Home >Backend Development >C++ >How to Draw Rectangles at Mouse Coordinates Using the C# Paint Event?

How to Draw Rectangles at Mouse Coordinates Using the C# Paint Event?

DDD
DDDOriginal
2024-12-31 09:17:14588browse

How to Draw Rectangles at Mouse Coordinates Using the C# Paint Event?

Using the Paint Event to Draw Shapes at Mouse Coordinates

When developing GUI applications, it is often necessary to draw shapes on the screen. One way to do this is to use the Paint event, which is raised when a portion of the control's surface needs to be redrawn.

Drawing a Rectangle

In the example code provided, the goal is to draw a rectangle at the coordinates of the mouse pointer. To achieve this, the DrawRect() method is used. This method takes the mouse coordinates and the PaintEventArgs object as arguments.

Modifying the Code

To include mouse coordinates in the Paint event, the code needs to be modified as follows:

  1. Add the argument of the MouseCoordinates to the PaintEvent:

    private void Form1_Paint(object sender, PaintEventArgs e, Point mouseCoordinates)
    {
    
    }
  2. Call the Drawing Function:
    In the Paint event handler, call the DrawRect() method with the provided mouse coordinates and the PaintEventArgs object:

    this.DrawRect(e, mouseCoordinates.X, mouseCoordinates.Y);

Complete Code

The complete code after modifications:

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;
        Point mouseCoord = new Point(x, y);
    }

    private void Form1_Paint(object sender, PaintEventArgs e, Point mouseCoord)
    {
        this.DrawRect(e, mouseCoord.X, mouseCoord.Y);
    }

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

Additional Considerations

When drawing on a control's surface, always use the Paint event or override the OnPaint method. Do not store the Graphics object, as it becomes invalid when the control is repainted. Instead, use the Graphics object provided by the PaintEventArgs object.

Additional resources for drawing shapes in C# include:

  • [Draw a Rectangle in C#](https://www.tutorialsteacher.com/csharp/csharp-draw-rectangle)
  • [Paint Event Tutorial in C#](https://www.c-sharpcorner.com/uploadfile/deba/paint-event-tutorial-in-csharp)

The above is the detailed content of How to Draw Rectangles at Mouse Coordinates Using the C# Paint Event?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn