Timer和图片框控件,编写不断向左移动的小动画

来源:互联网 发布:知乎和知网 编辑:程序博客网 时间:2024/05/20 06:26

代码:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication6{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private byte left;        private byte top;        private Random rd = new Random();                        private void Form1_Load(object sender, EventArgs e)        {            this.Text = "实验9-2:移动的考拉";            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;            pictureBox1.Image = Image.FromFile(@"d:\picture\kaola.jpg");            timer1.Enabled = true;            timer1.Interval = 500;            left = Convert.ToByte(rd.Next(255));            top= Convert.ToByte(rd.Next(255));                    }        private void timer1_Tick(object sender, EventArgs e)        {                                               if (pictureBox1.Right+left> this.ClientSize.Width)                {                    pictureBox1.Left = 0;                }                else if (pictureBox1.Bottom + top > this.ClientSize.Height)                {                    pictureBox1.Top = 0;                }                else                {                    pictureBox1.Left += left;                    pictureBox1.Top += top;                }        }    }}



运行结果:、



原创粉丝点击