窗体和控件的一个小实验:控制一个飞机左右移动

一:内容:

窗体上两个按钮:开始和停止,一个PictureBox,PictureBox中放一飞机图片。单击“开始”,则飞机图片左右在窗体上移动,移动到窗体边界时,反转方向移动。单击“停止”按钮,则飞机回到初始位置(窗体左侧)并停止移动。

窗体设置如下:


 int X, Y;
        public Form4()
        {
            InitializeComponent();
            X = pictureBox1.Location.X;
            Y = pictureBox1.Location.Y;
            
        }


timer1代码:

 private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Left += 10;
            if (pictureBox1.Left +pictureBox1.Width>= this.Width)
            {
                timer1.Enabled = false;
                timer2.Enabled = true;
            
            }
        }

timer2代码:

  private void timer2_Tick(object sender, EventArgs e)
        {
            pictureBox1.Left -= 10;
            if (pictureBox1.Left <= 0)
            {
                timer1.Enabled = true;
                timer2.Enabled = false;
              
            }
        }

开始代码:

    private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true; 
            timer2.Enabled = false;
        }



停止代码:

 private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            pictureBox1.Left = X;
            pictureBox1.Top = Y;
            timer2.Enabled = false;
        }
    }

运行图如下:


点停止如下:





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章