在form1的顶部,我做了:
private bool farwardbackward;
private FileInfo[] fi;
然后我有4个按钮点击事件:
正向:
private void button4_Click(object sender, EventArgs e)
{
farwardbackward = true;
button5.Enabled = true;
button6.Enabled = true;
timer5.Start();
}
落后:
private void button3_Click(object sender, EventArgs e)
{
farwardbackward = false;
button5.Enabled = true;
button6.Enabled = true;
if (current == -1)
current = fi.Length;
timer5.Start();
}
停止动画按钮:
private void button6_Click(object sender, EventArgs e)
{
timer5.Stop();
button6.Enabled = false;
button5.Text = "Pause Animation";
button5.Enabled = false;
Bitmap lastdownloadedimage = new Bitmap(fi[fi.Length -1].FullName);
pictureBox1.Image = lastdownloadedimage;
}
最后一个按钮是暂停/继续:
private void button5_Click(object sender, EventArgs e)
{
b = (Button)sender;
if (b.Text == "Pause Animation")
{
timer5.Enabled = false;
b.Text = "Continue Animation";
}
else
{
timer5.Enabled = true;
b.Text = "Pause Animation";
}
}
然后在timer5
tick事件中,它的间隔设置为1000毫秒我做了:
int current = -1;
private void timer5_Tick(object sender, EventArgs e)
{
if (farwardbackward)
current = Math.Min(current + 1, fi.Length - 1);
else
current = Math.Max(current - 1, 0);
if (fi.Length > 0)
{
Bitmap newbmp = new Bitmap(fi[current].FullName);
pictureBox1.Image = newbmp;
pictureBox1.Refresh();
newbmp.Dispose();
}
}
当我运行我的程序时,我总是在pictureBox1中看到变量fi中的最新/最后一张图像。 例如,fi中的第一个图像是000004.gif,最后一个图像是050122.gif 我看到了050122.gif
现在,将图像作为动画gif样式在pictureBox1中显示时应该是什么逻辑?
如果我第一次点击按钮继续前进,那么现在发生的是它跳转到图像000004.gif然后显示000005.gif等等......
如果我第一次点击后退按钮,那么它开始从050122.gif返回到050121.gif等等......
问题是,当我向后点击时,在我看到一些图像后,我向前点击按钮,然后它从图像继续向前移动,然后当它到达050122时它停止。它应该继续不停。这个想法是让它不停地移动。如果它到达050122.gif,我不希望它停止。如果它到达第一个图像或最后一个图像只是保持循环它不停止。但是出于某种原因050122.gif如果我向后移动时单击向前按钮就会停止。
如果我现在没错,那么问题就是当我在中间点击改变动画的方向时。然后,当它到达最后一张图像或第一张图像时,它会停止。我希望它只有在我点击停止按钮时才能继续不间断。
另一件事是我应该在停止按钮点击事件中做什么?应该重置一些值还是变量?
这是timer5 tick事件代码:
int current = -1;
private void timer5_Tick(object sender, EventArgs e)
{
if (fi.Length > 0)
{
if (farwardbackward)
{
current = (current >= fi.Length - 1) ? 0 : current++;
}
else
{
current = (current <= 0) ? fi.Length - 1 : current--;
}
pictureBox1.Image = new Bitmap(fi[current].FullName);
pictureBox1.Refresh();
}
}
当点击向前或向后移动时,我得到相同的异常:索引超出了数组的范围
System.IndexOutOfRangeException was caught
HResult=-2146233080
Message=Index was outside the bounds of the array.
Source=My Weather Station
StackTrace:
at mws.Form1.timer5_Tick(Object sender, EventArgs e) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Form1.cs:line 1317
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at mws.Program.Main() in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Program.cs:line 28
InnerException:
第1317行是:
pictureBox1.Image = new Bitmap(fi[current].FullName);
答案 0 :(得分:1)
我认为这对于你的tick方法来说是一个更好的代码:
if(farwardbackward){
current = (current >= fi.Length-1) ? 0 : current++;
}else{
current = (current <= 0) ? fi.Length-1 : current--;
}
picturebox1.Image = new Bitmap(fi[current].Fullname);
picturebox1.Refresh();
除此之外,我会稍微改变你的两个按钮的方法。
@γηράσκωδ'αείπολλάδιδασκόμε:thx,我错过了这一点
我的解决方案运行良好:
public partial class Form1 : Form
{
private string[] fi;
private int current = 0;
private Boolean forwardbackward = true;
private Boolean timerRunning = true;
public Form1()
{
InitializeComponent();
fi = Directory.GetFiles(@"C://temp/pics");
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (fi.Length > 0)
{
if (forwardbackward)
{
current = (current >= fi.Length - 1) ? 0 : ++current;
}
else
{
current = (current <= 0) ? fi.Length - 1 : --current;
}
pictureBox1.Image = new Bitmap(fi[current]);
pictureBox1.Refresh();
}
}
private void btn_changeDirection_Click(object sender, EventArgs e)
{
timer1.Stop();
forwardbackward = (forwardbackward) ? false : true;
timer1.Start();
}
private void btn_Pause_Continue_Click(object sender, EventArgs e)
{
if (timerRunning)
{
timer1.Stop();
timerRunning = false;
btn_Pause_Continue.Text = "Continue";
}
else
{
timer1.Start();
timerRunning = true;
btn_Pause_Continue.Text = "Pause";
}
}
private void btn_stop_Click(object sender, EventArgs e)
{
timer1.Stop();
}
}
没有完美的解决方案,但它应该做到的!