如何在单击按钮时绘制矩形?
How to draw a rectangle on a button click?
我想当我点击按钮时,在表单中添加一个矩形
我可以在 form paint 中添加我想要的数量,但我不能通过单击按钮添加像矩形这样的形状,我搜索了它但没有找到解决方案
这里有人知道怎么做吗?
这是我在 form paint 中的代码
private void Form1_Paint(object sender, PaintEventArgs e)
{
locationX = locationX + 20;
locationY = locationY + 20;
e.Graphics.DrawRectangle(Pens.Black,
new Rectangle(10 + locationX, 10 + locationY, 50, 30));
}
这是我的按钮代码
private void button1_Click(object sender, EventArgs e)
{
this.Paint += Form1_Paint;
}
但是当我点击按钮时它不起作用。为什么它不起作用?
要调用您需要括号的方法。
private void button1_Click(object sender, EventArgs e)
{
Form1_Paint(sender, e);
}
行
this.Paint += Form1_Paint;
将表单的事件 Paint
关联到函数 Form1_Paint。 它不会触发它。这是您只想执行 1 次的操作,而不是每次按下按钮时都执行的操作。
要触发Paint
事件,通常的方法是调用Form
class的Invalidate()
方法。 Invalidate其实是Control的一个方法。但是 Form
派生自 Control
,所以我们也可以访问 Form
中的方法。
所以在 Windows 表单中触发重绘的正确方法是将订阅放在 Load 方法中:
private void Form1_Load(object sender, EventArgs e)
{
this.Paint += Form1_Paint;
}
它应该已经隐藏在自动生成的代码中。
你的方法Form1_Paint
没问题
最后,按钮点击方法应该是:
private void button1_Click(object sender, EventArgs e)
{
this.Invalidate(); // force Redraw the form
}
Invalidate() : Invalidates the entire surface of the control and causes the control to be redrawn.
编辑:
用这个方法一次只能画1个矩形,因为整个surface都重画了,所以surface被完全擦掉了,然后就只画你在Form1_Paint方法中要求的了。
关于如何绘制多个矩形的答案,你应该创建一个矩形列表。在每次单击按钮时,您都会向列表中添加一个矩形,然后重新绘制所有矩形。
List<Rectangle> _rectangles = new List<Rectangle>();
private void button1_Click(object sender, EventArgs e)
{
locationX = locationX + 20;
locationY = locationY + 20;
var rectangle = new Rectangle(locationX, locationY, 50, 30));
this._rectangles.Add(rectangle);
this.Invalidate(); // force Redraw the form
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach(var rectangle in this._rectangles)
{
e.Graphics.DrawRectangle(Pens.Black, rectangle);
}
}
我想当我点击按钮时,在表单中添加一个矩形
我可以在 form paint 中添加我想要的数量,但我不能通过单击按钮添加像矩形这样的形状,我搜索了它但没有找到解决方案
这里有人知道怎么做吗?
这是我在 form paint 中的代码
private void Form1_Paint(object sender, PaintEventArgs e)
{
locationX = locationX + 20;
locationY = locationY + 20;
e.Graphics.DrawRectangle(Pens.Black,
new Rectangle(10 + locationX, 10 + locationY, 50, 30));
}
这是我的按钮代码
private void button1_Click(object sender, EventArgs e)
{
this.Paint += Form1_Paint;
}
但是当我点击按钮时它不起作用。为什么它不起作用?
要调用您需要括号的方法。
private void button1_Click(object sender, EventArgs e)
{
Form1_Paint(sender, e);
}
行
this.Paint += Form1_Paint;
将表单的事件 Paint
关联到函数 Form1_Paint。 它不会触发它。这是您只想执行 1 次的操作,而不是每次按下按钮时都执行的操作。
要触发Paint
事件,通常的方法是调用Form
class的Invalidate()
方法。 Invalidate其实是Control的一个方法。但是 Form
派生自 Control
,所以我们也可以访问 Form
中的方法。
所以在 Windows 表单中触发重绘的正确方法是将订阅放在 Load 方法中:
private void Form1_Load(object sender, EventArgs e)
{
this.Paint += Form1_Paint;
}
它应该已经隐藏在自动生成的代码中。
你的方法Form1_Paint
没问题
最后,按钮点击方法应该是:
private void button1_Click(object sender, EventArgs e)
{
this.Invalidate(); // force Redraw the form
}
Invalidate() : Invalidates the entire surface of the control and causes the control to be redrawn.
编辑:
用这个方法一次只能画1个矩形,因为整个surface都重画了,所以surface被完全擦掉了,然后就只画你在Form1_Paint方法中要求的了。
关于如何绘制多个矩形的答案,你应该创建一个矩形列表。在每次单击按钮时,您都会向列表中添加一个矩形,然后重新绘制所有矩形。
List<Rectangle> _rectangles = new List<Rectangle>();
private void button1_Click(object sender, EventArgs e)
{
locationX = locationX + 20;
locationY = locationY + 20;
var rectangle = new Rectangle(locationX, locationY, 50, 30));
this._rectangles.Add(rectangle);
this.Invalidate(); // force Redraw the form
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach(var rectangle in this._rectangles)
{
e.Graphics.DrawRectangle(Pens.Black, rectangle);
}
}