主題の要件:
タイマーと画像ボックスのコントロールを使用して、画像を左右に連続的に移動する小さなアニメーションを作成します。効果を図に示します。画像ファイル名はa.pngで、圧縮パッケージに含まれています。
請求:
(1)画像の左の値を変更して、画像を左(右)に移動します。
(2)RandomクラスのNextメソッドを使用して、Left値として数値[1、9]を生成します。
(3)画像をフォームの外に移動しないでください。左の値がフォームの範囲を超える場合は、画像を制御してフォーム内で左または右に移動できます。
参照コード:
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;
}
}