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