前段时间蛮火的哄老婆的小玩具

   主要就是几个要点:

    1、多线程

    2、按钮位置改变计算(不要超出界面)

    3、资源的使用(主要是只要一个exe就可以,不用多余的文件)

 

    直接代码吧,没啥好讲的。主要是我用来哄老婆的(* ̄︶ ̄)

  

    

 public partial class Form1 : Form
    {

        private bool IsForgive = false;
        private int index = 0;
        private List<Bitmap> files = new List<Bitmap> { Resource1.pic1, Resource1.pic2 };

        private Random random;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var thread = new Thread(PicChange);
            thread.IsBackground = true;
            thread.Start();
        }


        private void PicChange()
        {

            while (!IsForgive)
            {
                Image img= files[index++ % files.Count];
                pictureBox1.Image = img;
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                Thread.Sleep(1000);
            }
            pictureBox1.Image = Resource1.forgive;

        }

        private void button2_MouseMove(object sender, MouseEventArgs e)
        {
            random = new Random(DateTime.Now.Millisecond);
            var r1 = random.Next(100, 200);
            var r2 = random.Next() % 2 == 1 ? -1 : 1;
            var point = button2.Location;
            point.X = Math.Abs(point.X + (r1) * r2) % (this.Size.Width - button2.Size.Width);
            point.Y = Math.Abs(point.Y + (r1) * r2) % (this.Size.Height - button2.Size.Width);
            button2.Location = point;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button2.Hide();
            button1.Hide();
            IsForgive = true;
            Image image = Resource1.forgive;
            pictureBox1.Image = image;
        }
    }

效果界面:

原文地址:https://www.cnblogs.com/moshanghuakai/p/9441512.html