Windows应用编程(1)_10

题目要求:

利用Timer和图片框控件,编写一个图片不断向左向右移动的小动画。效果如图所示,图片文件名称为a.png,位于压缩包内。

要求:

(1)改变图片的Left值,图片向左(右)移动。

(2)利用Random类的Next方法产生一个[1,9]的数字作为Left值。

(3)图片不要移出窗体,如果Left值超出窗体范围,能控制图片在窗体内向左或向右移动。

参考代码:

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

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "移动的小球";
            this.pictureBox1.Image = Image.FromFile("a.jpg");
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            this.timer1.Enabled = true;
            this.timer1.Interval = 100;
        }
        Random rd = new Random();
        int p = 1, ind;

        private void timer1_Tick(object sender, EventArgs e)
        {
            ind = rd.Next(1, 9);
            if (ind * p > this.pictureBox1.Left || this.pictureBox1.Right + (-p * ind) >= this.Width)
            {
                p = -p;
            }
            this.pictureBox1.Left -= p * ind;
        }
    }
}

自动生成的Designer文件:

namespace WindowsFormsApplication5_10
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = global::WindowsFormsApplication5_10.Properties.Resources.a;
            this.pictureBox1.Location = new System.Drawing.Point(172, 86);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(42, 42);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(566, 251);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Timer timer1;
    }
}

猜你喜欢

转载自blog.csdn.net/wangws_sb/article/details/105627117