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

一:内容:

窗体上两个按钮:开始和停止,一个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;
        }
    }

运行图如下:


点停止如下:





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值