在Form1中我在构造函数中创建8个pictureBox:
pbs = new PictureBox[8];
progressbars = new ProgressBar[8];
for (int i = 0; i < pbs.Length; i++)
{
progressbars[i] = new ProgressBar();
progressbars[i].Size = new Size(100, 10);
progressbars[i].Margin = new Padding(0, 0, 0, 70);
progressbars[i].Dock = DockStyle.Top;
pbs[i] = new PictureBox();
pbs[i].MouseEnter += globalPbsMouseEnterEvent;
pbs[i].MouseLeave += globalPbsMouseLeaveEvent;
pbs[i].Tag = "PB" + i.ToString();
pbs[i].Size = new Size(100, 100);
pbs[i].Margin = new Padding(0, 0, 0, 60);
pbs[i].Dock = DockStyle.Top;
pbs[i].SizeMode = PictureBoxSizeMode.StretchImage;
Panel p = i < 4 ? panel1 : panel2;
p.Controls.Add(pbs[i]);
p.Controls.Add(progressbars[i]);
pbs[i].BringToFront();
progressbars[i].BringToFront();
}
在timer1 tick事件中,我将图像分配给一个循环中的pictureBox,使其看起来像动画。
private void timer1_Tick(object sender, EventArgs e)
{
try
{
for (int i = 0; i < file_array.Length; i++)
{
}
if (leave == true)
{
pb.Load(file_array[file_indxs]);
}
else
{
pbs[0].Load(file_array[file_indxs]);
}
file_indxs = file_indxs + 1;
if (file_indxs >= file_array.Length)
{
file_indxs = 0;
}
}
catch
{
timer1.Enabled = false;
}
}
file_array是string []我通过从目录中获取文件来创建它:
private void getfiles()
{
List<FileInfo> fileList = new List<FileInfo>();
for (int i = 0; i < BackgroundWorkerConfiguration.urlsDirectories.Count; i++)
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(BackgroundWorkerConfiguration.urlsDirectories[i]);
fileList.AddRange(di.GetFiles("*.*", System.IO.SearchOption.AllDirectories)
.Where(x => x.Length > 0).Select(y => y));
}
var file_array = fileList.OrderBy(x => x.CreationTime)
.GroupBy(x => x.DirectoryName)
.Select(g => g.Select(x => x.FullName).ToList())
.ToArray();
timer1.Enabled = true;
}
现在在计时器刻度事件中,我遍历file_array并将所有图像分配给第一个pictureBox:pbs [0]
但现在变量file_array不一样了。之前它只是字符串[],图像中有很多文件。
现在file_array是这样的:
索引0中的我有48个文件。 在索引1索引2索引和索引4中,每个索引中有61个文件。
我想在计时器刻度事件中将每个文件索引分配给另一个pictureBox。 所以file_array中的索引0应该将所有48个文件分配给pbs [0] 索引1文件应分配给pbs [1] ... 等等,直到索引4分配给pbs [4]
但我不想像在pbs [0]之前那样写.Load ... 我想它会自动将图像从file_array索引加载到pictureBoxes。 索引0中的第一个文件到第一个pictureBox,依此类推......
编辑**
public void globalPbsMouseEnterEvent(object sender, System.EventArgs e)
{
PictureBox p = sender as PictureBox;
if (p.Tag.ToString() == "PB0")
{
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Visible = true;
pb.BringToFront();
leave = true;
}
else
{
}
}
public void globalPbsMouseLeaveEvent(object sender, System.EventArgs e)
{
PictureBox p = sender as PictureBox;
if (p.Tag.ToString() == "PB0")
{
if (leave == true)
{
pb.Visible = false;
leave = false;
}
}
else
{
}
}
private void pb_MouseEnter(object sender, EventArgs e)
{
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Visible = true;
pb.BringToFront();
leave = true;
}
private void pb_MouseLeave(object sender, EventArgs e)
{
if (leave == true)
{
pb.Visible = false;
leave = false;
}
}
答案 0 :(得分:1)
只需将此pbs[0].Load(file_array[file_indxs]);
替换为pbs[file_indxs].Load(file_array[file_indxs]);
即可。请注意,file_indxs
应初始化为0
。
尝试使用文件列表输入为PictureBox设置动画:
//Use this custom PictureBox for convenience
public class AnimatedPictureBox : PictureBox {
List<string> imageFilenames;
Timer t = new Timer();
public AnimatedPictureBox(){
AnimateRate = 100; //It's up to you, the smaller, the faster.
t.Tick += Tick_Animate;
}
public int AnimateRate {
get { return t.Interval; }
set { t.Interval = value;}
}
public void Animate(List<string> imageFilenames){
this.imageFilenames = imageFilenames;
t.Start();
}
public void StopAnimate(){
t.Stop();
i = 0;
}
int i;
private void Tick_Animate(object sender, EventArgs e){
if(imageFilenames == null) return;
Load(imageFilenames[i]);
i = (i+1)%imageFilenames.Count;
}
}
//Now use the AnimatedPictureBox instead of the PictureBox
AnimatedPictureBox[] pbs = new AnimatedPictureBox[8];
//Animate all the PictureBoxes
for(int i = 0; i < file_array.Length; i++){
pbs[i].Animate(file_array[i]);
}
getfiles
应该返回List<string>
数组,将返回值分配给您在外部范围内定义的file_array
:
//Note that you now don't need your timer1, just remove it.
private List<string>[] getfiles() {
//....
return file_array;
}
//When calling getfiles,
//you have to assign the file_array variable to the return value
List<string>[] file_array; //your variable, you defined it as string[],
//but it won't work, we have to use List<string>[]
file_array = getfiles();
注意:如果要在Picturebox上停止动画,只需调用方法StopAnimate
即可。就是这样。