0

在 Form1 中,我在构造函数中创建了 8 个图片框:

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 滴答事件中,我将图像分配给循环中的图片框,因此它看起来像动画。

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 并将所有图像分配给第一个图片框:pbs[0]

但是现在变量 file_array 不一样了。在它只是字符串 [] 之前,图像中有许多文件。

现在 file_array 是这样的:

在索引 0 中,我有 48 个文件。在索引 1 索引 2 索引和索引 4 中,每个索引中有 61 个文件。

我想在计时器滴答事件中将文件的每个索引分配给另一个图片框。因此,file_array 中的索引 0 所有 48 个文件都应分配给 pbs[0] 索引 1 文件应分配给 pbs[1]... 依此类推,直到索引 4 分配给 pbs[4]

但我不想像在 pbs[0].Load 之前那样写...我希望它会自动将图像从 file_array 索引加载到图片框。索引 0 中的第一个文件到第一个图片框,依此类推...

编辑**

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;
            }
        }
4

1 回答 1

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。就这样。

于 2013-11-10T05:03:26.887 回答