Home >Backend Development >C++ >How to Draw Shapes in C# WinForms Using Mouse Coordinates and the Paint Event?

How to Draw Shapes in C# WinForms Using Mouse Coordinates and the Paint Event?

Linda Hamilton
Linda HamiltonOriginal
2025-01-02 13:17:38741browse

How to Draw Shapes in C# WinForms Using Mouse Coordinates and the Paint Event?

Utilizing the Paint Event to Draw Shapes Based on Mouse Coordinates

When creating interactive graphical applications, it is essential to be able to draw shapes based on user input. In C# WinForms, the Paint event provides a mechanism for drawing content onto the application's surface.

Integrating Mouse Coordinates

To incorporate mouse coordinates into the drawing process, the Form1_MouseMove event is typically used. Within this event, the X and Y coordinates can be extracted from the MouseEventArgs object. These coordinates represent the current position of the mouse pointer within the application's window.

Calling DrawRect() with Multiple Arguments

Your code sample has a DrawRect() method that takes multiple arguments, including the mouse coordinates and a PaintEventArgs object. To call this method from within the Form1_MouseMove event, you can modify your code as follows:

<br>private void Form1_MouseMove(object sender, MouseEventArgs e)<br>{</p>
<pre class="brush:php;toolbar:false">int x = e.X; 
int y = e.Y;
DrawRect(e.Graphics, x, y); // Use the Graphics object provided by PaintEventArgs

}

Paint Event and DrawRect()

Within the Form1_Paint event handler, the Graphics object is provided by the PaintEventArgs object. This Graphics object is used to draw the shape onto the application's surface.

In your case, the DrawRect() method can use the provided Graphics object to draw the rectangle at the specified coordinates:

<br>public void DrawRect(Graphics g, int x, int y)<br>{</p>
<pre class="brush:php;toolbar:false">Pen pen = new Pen(Color.Azure, 4);
Rectangle rect = new Rectangle(x, y, rex, rey);
g.DrawRectangle(pen, rect);

}

Complete Example

Putting these components together, the complete code for your application would look like this:

<br>using System;<br>using System.Drawing;<br>using System.Windows.Forms;</p>
<p>public partial class Form1 : Form<br>{</p>
<pre class="brush:php;toolbar:false">private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    int x = e.X; 
    int y = e.Y;
    DrawRect(e.Graphics, x, y);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{

}

public void DrawRect(Graphics g, int x, int y)
{
    Pen pen = new Pen(Color.Azure, 4);
    Rectangle rect = new Rectangle(x, y, rex, rey);
    g.DrawRectangle(pen, rect);
}

}

By utilizing the Paint event and the DrawRect() method, you can effectively draw shapes onto the application's surface based on the user's mouse coordinates.

The above is the detailed content of How to Draw Shapes in C# WinForms Using Mouse Coordinates and the 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