C# 更有效地处理多个图片框
我有12个像幻灯片一样运行的图片框,使用以下简单代码:C# 更有效地处理多个图片框,c#,C#,我有12个像幻灯片一样运行的图片框,使用以下简单代码: if (pictureBox1.Visible == true) { pictureBox1.Visible = false; pictureBox2.Visible = true; } 一直重复到12点,然后计时器停止,我对这一点很满意 但我努力保持它的美丽。。。它真的渴望简单的事情。 例如,单击按钮1时: private void button4_Click(object sender, EventArgs e) { // ple
if (pictureBox1.Visible == true)
{
pictureBox1.Visible = false;
pictureBox2.Visible = true;
}
一直重复到12点,然后计时器停止,我对这一点很满意
但我努力保持它的美丽。。。它真的渴望简单的事情。
例如,单击按钮1时:
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();
}
以上代码在提供剧集等之前播放一个幻灯片。
然后,对于按钮2:
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;
在timer tick事件处理程序中,执行如下操作
if (_currentImageIndex < NumberOfImages) {
pictureBox1.Image = _images[_currentImageIndex];
_currentImageIndex++;
} else {
// play sound or whatever you need to do here
_currentImageIndex = 0;
}
if(\u currentImageIndex
请注意,我将不同的图像依次指定给单个图片框,以创建动画效果,而不是有许多不同的图片框
下面是一个完整的工作示例。请注意,我已经更改了一些细节。做事总有几种方法:
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;
}
}
}
公共部分类格式:表单
{
私有图像[]_图像;
int_currentImageIndex;
公共信息()
{
初始化组件();
_图像=新图像[]{
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();
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
如果(_currentImageIndex<_images.Length){
pictureBox1.Image=\u images[\u currentImageIndex];
_currentImageIndex++;
}否则{
_currentImageIndex=0;
}
}
}
您还必须在表单上放置一个计时器
组件,并将其间隔
设置为适当的毫秒数。双击计时器图标,创建timer1\u Tick
事件处理程序。注意,我添加了一个timer1.Start()在窗体的构造函数中编写>以启动计时器
当然,您必须已将图像资源添加到项目中。我去掉了常量int NumberOfImages
,改为使用数组初始值设定项。这样,阵列的大小就会自动调整到正确的长度(\u images.length
)
您询问了有关数组和索引的问题。你可以把一个阵列想象成一件有许多抽屉的家具。您可以通过指定每个抽屉的索引来访问它。第一个抽屉的索引为0(\u images[0]
);最后一个是数组长度减1(\u images[\u images.length-1]
)。如果数组的长度为N
,则索引的范围为[0…N-1]
还有一个标题难看的难看问题。你需要学习如何使用数组和循环…你真的想知道pictureBox3.Image=Properties.Resources代码>?比如pictureBox3.Image=Properties.Resources.picture03
@Rahul,是的,很抱歉,它不让我放任何东西,比如“如何让多个图片盒编码更干净”它说像编码这样的词是不允许的等等,我对这一切还是很陌生,所以让我理解uve说的话。。。。你说使用1个图片框,然后我添加一个“const”事件(?不知道怎么称呼它),事件数量为12个图片(这是正确的。)然后在该事件中“我插入所有图片,因此代码将实际启动”\u images[0]。image“然后是我的图片位置,还是代码的第一部分会有所不同?我不知道指数,所以我要好好读一读。我可以理解“else”部分,但“if”有什么作用,它会在计时器刻度上逐渐改变图片吗?我刚刚尝试了上述方法,没有错误,但不起作用,我尝试过将这些行放在任何空白处(PictureBoxClicl计时器刻度等)显示没有错误,但不会显示任何图片,如果我将它与“Image[]\u Image…”下面的代码一起放在分部类中,它会显示为错误,我缺少什么_图像[0]=Properties.Resources.ancater\u home\u区域_images[1]=Properties.Resources.other_Trap_由_Masaki1812编写_images[2]=Properties.Resources.backgroundDefault\u jpg;