简体   繁体   English

按钮单击Windows窗体C#

[英]button click windows form c#

Hi guys I'm trying to make a "minipaint" application which has three buttons(rectangle, circle and line). 嗨,大家好,我正在尝试制作一个具有三个按钮(矩形,圆形和直线形)的“ minipaint”应用程序。 I'm having problem with making my buttons work. 我在使buttons正常工作时遇到问题。 For example I have this rectangle class which inherits color, thickness, startpoints x, y from shape: 例如,我有这个矩形类,它从形状继承了颜色,厚度,起点x,y:

class rectangle : shape
{
    public int length { get; set; }
    public int width { get; set; }

    public override void Draw(Graphics g)
    {
        g.DrawRectangle(new Pen(color), new Rectangle(startx, starty, width, length));
    }
}

Now I want my rectangle_btn_Click to print a rectangle in my panel whenever I click on it. 现在,我希望我的rectangle_btn_Click每次在panel上打印一个矩形。 here is my panel code: 这是我的panel代码:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = panel1.CreateGraphics();
}

and this is my button : 这是我的button

private void rectangle_btn_Click(object sender, EventArgs e)
{
    rectangle r = new rectangle();
    int retval = r.Draw(g);
}

But it has an error and it does not recognize g . 但是它有一个错误,并且不能识别g How should I make this work? 我应该如何做这项工作?

You need to declare your Graphics object globally: 您需要全局声明您的Graphics对象:

private Graphics g; 

private void panel1_Paint(object sender, PaintEventArgs e)
{
    g = panel1.CreateGraphics();
}

Then this should also work 然后这也应该工作

private void rectangle_btn_Click(object sender, EventArgs e)
{
    rectangle r = new rectangle();
    r.Draw(g);
}

This assumes panel1_Paint and rectangle_btn_Click are both declared in the same class. 假定panel1_Paintrectangle_btn_Click都在同一类中声明。

EDIT: 编辑:

As @krw12572 pointed out the problem with this is that after minimizing the form the drawn object will disappear because the panel will be redrawn. 正如@ krw12572指出的那样,问题在于最小化表单后,绘制的对象将消失,因为将重新绘制面板。 To solve the problem following edits need to be made: 要解决此问题,需要进行以下编辑:

private List<shape> shapes = new List<shape>(); 

private void panel1_Paint(object sender, PaintEventArgs e)
{
    foreach (var shape in shapes) {
       shape.Draw(e.Graphics);
    }
}

private void button1_Click(object sender, EventArgs e) 
{   
    //This will however draw a rectangle at a fixed position with a fixed size         
    rectangle r = new rectangle() {startx = 10, starty = 10, length = 10, width = 10, color = Color.Black};
    shapes.Add(r);
    panel1.Invalidate();
}

Also the classes should look something like this: 这些类也应如下所示:

public class shape
{
    public Color color { get; set; }
    public int width { get; set; }
    public int startx { get; set; }
    public int starty { get; set; }

    public virtual void Draw(Graphics g)
    {

    }
}

public class rectangle : shape
{
    public int length { get; set; }
    public int width { get; set; }
    public override void Draw(Graphics g)
    {
        g.DrawRectangle(new Pen(color), new Rectangle(startx, starty, width, length));
    }
}

This approach uses a cache with all objects that need to be drawn. 这种方法将缓存与所有需要绘制的对象一起使用。 On button click a object is added to the cache. 在按钮上单击,将对象添加到缓存。

You should perform any painting only in Paint event handler. 您只能在Paint事件处理程序中执行任何绘画。 Use graphics object from Paint event handler. 使用Paint事件处理程序中的图形对象。

Implementing this way may be tricky but whenever your panel is redrawn, your painted shape will disappear if you don't perform painting in Paint event. 实施此方法可能很棘手,但是只要不在Paint事件中执行绘画操作,只要重新绘制面板,绘制的形状就会消失。

private shape _shape;

private void panel1_Paint(object sender, PaintEventArgs e)
{
    _shape.Draw(e.Graphics);
}

private void rectangle_btn_Click(object sender, EventArgs e)
{
    _shape = new rectangle();
    panel1.Invalidate();
}

Update: Above answer is assuming you have Draw(Graphics g) method in your base class shape and it's overridden/implemented in rectangle class. 更新:上面的答案是假设您在基类shape具有Draw(Graphics g)方法,并且该方法在rectangle类中被覆盖/实现。

You should either declare your Graphics variable 'g' inside rectangle_btn_click or at the class level outside any methods scope. 您应该在矩形_btn_click内或在任何方法范围之外的类级别声明图形变量'g'。 Then use it inside your stubs. 然后在存根中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM