如何在按钮单击上绘制矩形? [英] How to draw a rectangle on a button click?

查看:107
本文介绍了如何在按钮单击上绘制矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击按钮时在表单中添加一个矩形

我可以在表单绘画中添加所需的数量,但是我无法通过单击按钮添加矩形之类的形状,因此我进行了搜索但是我没有找到解决方案

有人知道该怎么做吗?

I want when I click button, add one rectangle to the form
I can add in form paint how much I want but I can't add shape like rectangle by click button and I searched about it but I didn't find a solution for it
is here somebody know how to do it?

这是我在窗体上绘制的代码

This is my code in 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;
    }

,但是当我单击按钮时它不起作用。为什么它不起作用?

but its not working when I click button. why its not working?

推荐答案

 this.Paint += Form1_Paint;

将表单的事件 Paint 与您的函数Form1_Paint。 它不会触发。这是您只想执行1次,而不是每次按下按钮时都要执行的操作。

Associate the event Paint of your Form to your function Form1_Paint. It doesn't trigger it. This is something you want to do only 1 time, not everytime you hit a button.

要触发 Paint 事件,通常的方法是调用 Form <的 Invalidate()方法。 / code>类。实际上,Invalidate是 Control 的方法。但是 Form 源自 Control ,因此我们可以访问 Form

To trigger the Paint event, the usual way is to call the Invalidate() method of the Form class. In fact, Invalidate is a method of Control. But Form derivate from Control, so we have access to the method in Form too.

所以在Windows窗体中触发重新绘制的正确方法是将subscribe放入Load方法:

So the right way to trigger a repaint in Windows Forms is to put the subscribe in the Load method :

private void Form1_Load(object sender, EventArgs e)
{
    this.Paint += Form1_Paint;
}

它应该已经隐藏在自动生成的代码中。
您的方法 Form1_Paint 可以。

It should already be hidden in the auto generated code. Your method Form1_Paint is ok.

最后,单击按钮的方法应为:

Finally, the button click method should be :

private void button1_Click(object sender, EventArgs e)
{
    this.Invalidate(); // force Redraw the form
}

从文档中


Invalidate():无效

Invalidate() : Invalidates the entire surface of the control and causes the control to be redrawn.

编辑:

使用此方法,一次只能绘制1个矩形,因为整个表面都已重画,因此该表面被完全擦除,然后仅绘制您在Form1_Paint中要求的内容

With this method, you can draw only 1 rectangle at a time, because the whole surface is redrawn, so the surface is completly erase, and then it draws only what you asked in the Form1_Paint method.

有关如何绘制多个矩形的答案,应创建一个矩形列表。在每个单击按钮时,将一个Rectangle添加到列表中,然后重新绘制所有矩形。

For the answer on how to draw multiple rectangles, you should create a List of Rectangle. At each click button, you add a Rectangle to the list, and you redraw all the rectangles.

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);
    }
}

这篇关于如何在按钮单击上绘制矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆