可以手動處理動畫事件。令人驚訝的是,OnFrameChanged
事件仍然激發,使動畫成爲可能。
public class Form1 : Form {
Button btn = new Button { Text = "Button", Dock = DockStyle.Top };
AsyncPictureBox box = new AsyncPictureBox("c:\\temp\\chick_dance.gif") { Dock = DockStyle.Fill };
public Form1() {
Controls.Add(box);
Controls.Add(btn);
btn.Click += delegate {
box.AnimateImage = !box.AnimateImage;
Thread.Sleep(30000);
};
}
}
public class AsyncPictureBox : Control {
Bitmap bitmap = null;
bool currentlyAnimating = false;
int frameCount = 0;
int frame = 0;
public AsyncPictureBox(String filename) {
bitmap = new Bitmap(filename);
this.DoubleBuffered = true;
frameCount = bitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Time);
}
public bool AnimateImage {
get {
return currentlyAnimating;
}
set {
if (currentlyAnimating == value)
return;
currentlyAnimating = value;
if (value)
ImageAnimator.Animate(bitmap, OnFrameChanged);
else
ImageAnimator.StopAnimate(bitmap, OnFrameChanged);
}
}
// even though the UI thread is busy, this event is still fired
private void OnFrameChanged(object o, EventArgs e) {
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
bitmap.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Time, frame);
frame = (frame + 1) % frameCount;
g.DrawImage(bitmap, Point.Empty);
g.Dispose();
}
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
if (disposing) {
if (bitmap != null)
bitmap.Dispose();
bitmap = null;
}
}
}
如果你註釋掉了添加按鈕的代碼,gif動畫呢? – Bijington 2015-02-09 10:10:11
爲什麼你需要300個按鈕?也許有更好的解決方案? – JeffRSon 2015-02-09 10:29:09
@Bijington是的,它會動畫 – Fish 2015-02-09 13:08:38