C#程序設計(二十一)----編寫一個圖片不斷向左移動的小動畫

* 程序的版權和版本聲明部分
* Copyright (c) 2012, 煙臺大學計算機學院學生
* All rights reserved.

* 作 者: 劉鎮
* 完成日期: 2012 年 11 月 10 日
* 版 本 號: 3.021

* 對任務及求解方法的描述部分

* 問題描述:

利用Timer和圖片框控件,編寫一個圖片不斷向左移動的小動畫。所用圖片自行提供或設計。

 

*代碼部分:

 

 

 

 

 

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 win9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Point p = pictureBox1.Location;

            if (pictureBox1.Location.X != 460)
            {
                p.X++;
            }
            else
            {
                p.X = 0;
            }
            pictureBox1.Location = p;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

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

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}


 

 

 

測試結果;

 

 

 

 

 

 

 

心得經驗:

 

一、難題就是直接改變圖片的位置Location是按不到的,Location不是可以調用的方法;因此可以通過引入Point座標點類,用來修改Location,然後就是將Timer的Tick事件加以處理,讓他按指定時間間隔移動點的座標。

 

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