formpicturebox.cs

来自「csharp课本的源代码」· CS 代码 · 共 70 行

CS
70
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PictureBoxExample
{
    public partial class FormPictureBox : Form
    {
        Bitmap[] bitmap = new Bitmap[8];
        int num = 0;
        int onceMove = 10;

        public FormPictureBox()
        {
            InitializeComponent();
        }

        private void FormPictureBox_Load(object sender, EventArgs e)
        {
            this.BackColor = Color.White;
            myTimer.Interval = trackBar1.Value;
            for (int i = 1; i <= 8; i++)
            {
                bitmap[i - 1] = new Bitmap(Application.StartupPath + @"\images\t" + i.ToString() + ".jpg");
            }
            myTimer.Enabled = false;

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            myTimer.Interval = trackBar1.Value;
        }

        private void myTimer_Tick(object sender, EventArgs e)
        {
            num++;
            int num1 = (num + 1) % 8;
            int num2 = (num + 2) % 8;
            int num3 = (num + 3) % 8;
            pictureBoxSmall.Image = bitmap[num1];
            pictureBoxMiddle.Image = bitmap[num2];
            pictureBoxLarge.Image = bitmap[num3];
            Random r = new Random();
            pictureBoxSmall.Left = (pictureBoxSmall.Left + r.Next(onceMove)) % Width;
            pictureBoxMiddle.Left = (pictureBoxMiddle.Left + r.Next(onceMove)) % Width;
            pictureBoxLarge.Left = (pictureBoxLarge.Left + r.Next(onceMove)) % Width;

        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            myTimer.Enabled = true;
        }

        private void buttonContinue_Click(object sender, EventArgs e)
        {
            myTimer.Enabled = true;
        }

        private void buttonPause_Click(object sender, EventArgs e)
        {
            myTimer.Enabled = false;
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?