更有效地处理多个图片框

时间:2015-09-11 21:04:20

标签: c#

我有12个图片框像幻灯片一样运行,使用简单的代码:

if (pictureBox1.Visible == true)
{
 pictureBox1.Visible = false;
 pictureBox2.Visible = true;
}

并一直重复到12,然后计时器停止,我很高兴这一点。

但是我努力保持它漂亮...它对于简单的事情来说真的很长。 例如,当点击button1时:

private void button4_Click(object sender, EventArgs e)
{ // please teacher
    BackgroundImage = Properties.Resources.please;
    button1.Visible = false;
    button4.Visible = false;
    timer2.Start();
    pictureBox1.Image = Properties.Resources.please;
    pictureBox2.Image = Properties.Resources.PleaseTeacher1;
    pictureBox3.Image = Properties.Resources.pleaseTeacher_023;
    pictureBox4.Image = Properties.Resources;
    pictureBox4.Image = Properties.Resources;
    pictureBox5.Image = Properties.Resources;
    pictureBox6.Image = Properties.Resources;
    pictureBox7.Image = Properties.Resources;
    pictureBox8.Image = Properties.Resources;
    pictureBox9.Image = Properties.Resources;
    pictureBox10.Image = Properties.Resources;
    pictureBox11.Image = Properties.Resources;
    pictureBox12.Image = Properties.Resources;
    pictureBox1.Visible = true;
    SoundPlayer audio = new SoundPlayer(slideshow_test.Properties.Resources.Please_Teacher_Opening);
    audio.Play();
}

以上代码在播放剧集等之前播放一个幻灯片。 然后再次为button2:

private void button4_Click(object sender, EventArgs e)
{ // show2
        BackgroundImage = Properties.Resources.show2;
        button1.Visible = false;
        button4.Visible = false;
        timer2.Start();
        pictureBox1.Image = Properties.Resources;
        pictureBox2.Image = Properties.Resources;
        pictureBox3.Image = Properties.Resources;
        pictureBox4.Image = Properties.Resources;
        pictureBox4.Image = Properties.Resources;
        pictureBox5.Image = Properties.Resources;
        pictureBox6.Image = Properties.Resources;
        pictureBox7.Image = Properties.Resources;
        pictureBox8.Image = Properties.Resources;
        pictureBox9.Image = Properties.Resources;
        pictureBox10.Image = Properties.Resources;
        pictureBox11.Image = Properties.Resources;
        pictureBox12.Image = Properties.Resources;
        pictureBox1.Visible = true;
        SoundPlayer audio = new SoundPlayer(slideshow_test.Properties.Resources.Please_Teacher_Opening);
        audio.Play();
} 

这就完成了我希望它做的工作,但有没有办法让这段代码更容易阅读以供将来参考(因为我还有10个节目需要添加)?

1 个答案:

答案 0 :(得分:2)

声明一个图像数组

private const int NumberOfImages = 12;
private Image[] _images = new Image[NumberOfImages];

在表格打开时填写图片

_images[0] = Properties.Resources.myPicture_00;
_images[1] = Properties.Resources.myPicture_01;
_images[2] = Properties.Resources.myPicture_02;
_images[3] = Properties.Resources.myPicture_03;
...

同时声明当前图像的索引

int _currentImageIndex;

在你的计时器刻度事件处理程序中执行类似这样的操作

if (_currentImageIndex < NumberOfImages) {
    pictureBox1.Image = _images[_currentImageIndex];
    _currentImageIndex++;
} else {
    // play sound or whatever you need to do here
    _currentImageIndex = 0;
}

请注意,我将依次将不同的图像分配到单个图片框,以创建动画效果,而不是拥有许多不同的图片框。

这是一个完整的工作示例。请注意,我已经更改了一些细节。总有几种做事方式:

public partial class frmAnimation : Form
{
    private Image[] _images;
    int _currentImageIndex;

    public frmAnimation()
    {
        InitializeComponent();
        _images = new Image[] {
            Properties.Resources.st_anim_frame0,
            Properties.Resources.st_anim_frame1,
            Properties.Resources.st_anim_frame2,
            Properties.Resources.st_anim_frame3,
            Properties.Resources.st_anim_frame4
        };
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (_currentImageIndex < _images.Length) {
            pictureBox1.Image = _images[_currentImageIndex];
            _currentImageIndex++;
        } else {
            _currentImageIndex = 0;
        }
    }
}

您还必须在表单上放置Timer组件,并将其Interval设置为适当的毫秒数。双击计时器图标创建timer1_Tick事件处理程序。请注意,我在表单的构造函数中添加了timer1.Start();以启动计时器。

当然,您必须为项目添加图像资源。我摆脱了const int NumberOfImages并使用了数组初始化器。这样,数组的大小自动调整为正确的长度(_images.Length)。

您询问了数组和索引。您可以将阵列想象成具有多个抽屉的家具。您可以通过指定其索引来访问每个抽屉。第一个抽屉的索引为0(_images[0]);最后一个,数组长度减去1(_images[_images.Length - 1])。如果数组的长度为N,则索引的范围为[0 ... N - 1]

enter image description here