C#利用Timer控件实现图片运动

界面如图:

一个开始button;停止button;picture控件;timer控件


代码:

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 课题二转换区仿真
{
    public partial class Form1 : Form
    {
        Boolean b = true;
        private int startY = 0;

        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;

        }

      
        private void timer1_Tick(object sender, EventArgs e)
        {
            int i = pictureBox1.Location.X + 10;//图片坐标移动
           
            if (i>this.Width)
            {
                i = 0;
            }
            startY = pictureBox1.Location.Y;
          
            pictureBox1.Location = new Point(i, startY);
            pictureBox1.Refresh();//不断刷新
            timer1.Start(); 

           
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;//timer控件初始化
            timer1.Interval = 100;
        }  
        private void button1_Click(object sender, EventArgs e)//开始
        {
            timer1.Start(); 

        }

        private void button2_Click(object sender, EventArgs e)//停止
        {
            timer1.Stop();
               
        }

      
    }
}


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页