繁体   English   中英

更有效地处理多个图片框

[英]Handling multiple picture boxes more efficiently

我有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个要添加的节目)?

声明一组图像

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]

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM