如何使用C#在按钮单击时将相同图片的另一张图片添加到我的表单中?

时间:2018-05-31 00:08:06

标签: c# windows windows-forms-designer system.drawing

我创建了一个Windows窗体,当我点击一个按钮时,它会显示一只熊猫在移动。 当我添加一只熊猫时,它可以工作,但我希望在点击按钮时出现另一只熊猫。当我再次单击按钮以显示另一只熊猫时,我正试图!当我点击按钮时,我的熊猫消失并再次从它的起点重新出现并开始移动!

(例如点击按钮3次=在我的表格中移动3只熊猫)

该类的代码名为" panda":



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace test_moving_pic
{   
    class panda
    {
        public Image img_panda;
        public Rectangle rect_panda;
        
        public panda(Image img, Rectangle rect)
        {
            this.img_panda = img;
            this.rect_panda = rect;
        }       
    }
}




这是我用于表单的代码:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test_moving_pic
{
    public partial class Form1 : Form
    {
        Image image;
        Rectangle rect;

        int direction = 3;
        public Form1()
        {
            InitializeComponent();           
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
           Graphics g = e.Graphics;
            if (image != null && rect != null)
                g.DrawImage(image, rect);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            rect.X += this.direction;
            rect.Y += this.direction;

            if (rect.X <= 100 && rect.Y <= 100)
            {
                rect.X += this.direction;
                rect.Y += this.direction;
            }
            else
            {
                rect.Y += this.direction;
                if (rect.Y >= 100)
                {
                    rect.Y = 100;
                    rect.X += this.direction;
                }
            }
            
            Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panda p = new panda(Image.FromFile("C:\\Users\\hsnha\\OneDrive\\Desktop\\Panda.png"), new Rectangle(20, 20, 70, 70));
            image = p.img_panda;
            rect = p.rect_panda;
        }
    }
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

Form1_Paint只会吸引一只熊猫。你需要以某种方式保存你创建的所有熊猫,并在Paint方法中绘制所有的大熊猫!您还需要在Tick中更新所有大熊猫的位置。

例如: 在班级List<panda> pandas;

中定义成员
    private void button1_Click(object sender, EventArgs e)
    {
        panda p = new panda(Image.FromFile("C:\\Users\\hsnha\\OneDrive\\Desktop\\Panda.png"), new Rectangle(20, 20, 70, 70));
        pandas.Add(p);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        foreach (panda p in pandas)
        {
            Rectangle rect = p.rect_panda;
            // Fix the rect like before
        }
    }

同样对于Draw,循环大熊猫并画出每个。这有助于你解开吗?

答案 1 :(得分:0)

试试这个:

public partial class Form1 : Form
{
    List<panda> pandaList = new List<panda>();
    int direction = 3;

    class panda
    {
        public Image img_panda;
        public Rectangle rect_panda;

        public panda(Image img, Rectangle rect)
        {
            this.img_panda = img;
            this.rect_panda = rect;
        }
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        foreach (panda p in pandaList)
            g.DrawImage(p.img_panda, p.rect_panda);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        foreach (panda p in pandaList)
        {
            p.rect_panda.X += this.direction;
            p.rect_panda.Y += this.direction;

            if (p.rect_panda.X <= 100 && p.rect_panda.Y <= 100)
            {
                p.rect_panda.X += this.direction;
                p.rect_panda.Y += this.direction;
            }
            else
            {
                p.rect_panda.Y += this.direction;
                if (p.rect_panda.Y >= 100)
                {
                    p.rect_panda.Y = 100;
                    p.rect_panda.X += this.direction;
                }
            }

        }
        Invalidate();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        pandaList.Add(new panda(Image.FromFile(@"C:\Users\hsnha\OneDrive\Desktop\Panda.png"), new Rectangle(20, 20, 70, 70)));
    }
}