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
Release: 2025-01-02 13:17:38
Original
741 people have browsed it

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>
<div class="code" style="position:relative; padding:0px; margin:0px;"><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
Copy after login

}

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>
<div class="code" style="position:relative; padding:0px; margin:0px;"><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);
Copy after login

}

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>
<div class="code" style="position:relative; padding:0px; margin:0px;"><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);
}
Copy after login

}

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!

source:php.cn
Previous article:How to Sort Strings Alphabetically and Numerically Using a Custom Comparer in C#? Next article:How to Prevent Entity Framework from Saving Child Entities When Saving a Parent Entity?
Statement of this Website
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
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template