如何使用 Paint 事件在鼠标坐标处绘制形状 [英] How to use the Paint event to draw shapes at mouse coordinates
问题描述
我最近显然开始使用 C#
编程,并试图做一个简单的 WinForms
应用程序,该应用程序采用鼠标坐标并根据坐标缩放矩形.
我面临的问题是我不知道如何调用使用更多参数的方法(在这种情况下是 x
、y
和 PaintEventArgs
).或者,我知道如何处理 PaintEvent
.
这是整个代码,因为它非常短而且相当简单:
使用系统;使用 System.Drawing;使用 System.Windows.Forms;公共部分类 Form1 :表单{public void Form1_MouseMove(对象发送者,MouseEventArgs e){int x = e.X;int y = e.Y;字符串数据 = (x.ToString() + " " + y.ToString());DrawRect(Something, x, y);}PaintEventArgs pEventArgs;private void Form1_Paint(对象发送者,PaintEventArgs e){}public void DrawRect(PaintEventArgs e, int rey, int rex){图形 gr = e.Graphics;Pen pen = new Pen(Color.Azure, 4);矩形 rect = new Rectangle(0, 0, rex, rey);gr.DrawRectangle(pen, rect);}}
我正在尝试调用 DrawRect()
方法来根据鼠标坐标绘制具有 width
和 height
的矩形.
那么我怎样才能用坐标和PaintEventArgs
调用DrawRect()
?
PaintEventArgs
允许您访问 Graphics
对象,您需要该对象来绘制一些东西.
如果您不想使用PaintEventArgs
,我建议您调用Form
的CreateGraphics()
方法,然后它将允许您绘制矩形.
为了提高性能,我建议您使用 using(...){ } 键来处理 Graphics
对象和 Pen
对象.>
您需要包含 System.Drawing
才能使用 Graphics
和 Pen
.
你的代码看起来像这样:
使用 System.Drawing;使用 System.Text;使用 System.Threading.Tasks;使用 System.Windows.Forms;命名空间 WindowsFormsApp2{公共部分类 Form1 :表单{点_坐标;公共 Form1(){this._coordinates = new Point();this.InitializeComponent();}private void Form1_Load(对象发送者,EventArgs e){}public void Form1_MouseMove(对象发送者,MouseEventArgs e){this._coordinates = new Point(e.X, e.Y);this.Invalidate();}private void Form1_Paint(对象发送者,PaintEventArgs e){//不要在第一个 Paint 事件上绘制if(this._coordinates.X != 0 && this._coordinates.Y != 0){this.DrawRect(e);}}public void DrawRect(PaintEventArgs e){使用 (Pen pen = new Pen(Color.Azure, 4)){矩形 rect = new Rectangle(0, 0, this._coordinates.X, this._coordinates.Y);e.Graphics.DrawRectangle(pen, rect);}}}}
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
?
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);
}
}
}
}
这篇关于如何使用 Paint 事件在鼠标坐标处绘制形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!