編輯:我在下面添加了另一個答案。我發現這個問題的答案很複雜。
JRoughan可能是正確的可能有更好的方法來做到這一點。但根據你的要求,我寫了一個類,它會提供你提供的圖像,並一次將它們繪製到圖形對象上,然後清除圖形對象。
我在一個Method中實例化了Class,所以不能保證它不會超出範圍並在完全運行之前收集,爲了防止這種情況發生,您可能希望使其成爲全局的,然後在每次重新實例化時想要使用它,與新的圖像。但是如果你調用它兩次,第一次會失敗,所以我發現這個方法更好,它取決於上下文,你打算一次使用多少不同的動畫等......我還添加了一個ImageindexChanged事件,可以用於瞭解圖像在課堂之外何時發生變化。請享用!我希望這是你想要的。
我剛剛添加了一個按鈕和麪板。追平了按鈕單擊事件到Button1_Click方法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CustomControl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//insert your images in this next line
}
private void button1_Click(object sender, EventArgs e)
{
Image[] explodingImages = new Image[] { Properties.Resources.explode1, Properties.Resources.explode2, Properties.Resources.explode3 };
//initialize the explosion with the images from above
Sprites Explosion = new Sprites(explodingImages,panel1.CreateGraphics());
//the timer will start and it will draw all images from the above collection, then clear itself, then dispose of itself.
Explosion.ImageIndex = 0;
}
}
public class Sprites
{
private Image _DisplayedImage;
public Image DisplayedImage
{
get { return _DisplayedImage; }
}
private Image[] Images{get;set;}
private int _ImageIndex = 0;
public event EventHandler ImageIndexChanged;
public int ImageIndex
{
get { return _ImageIndex; }
set
{
//Changes thh ImageIndexNumber
_ImageIndex = value;
//Updates the Displayed Image with the current Image as per index
_DisplayedImage = Images[ImageIndex];
//fires the ImageIndexChanged Event (if required)
if (ImageIndexChanged != null)
ImageIndexChanged(this, EventArgs.Empty);
//draws the explosion to the graphics object
DrawExplosion(G, 0, 0);
//starts the timer
changeImageTimer.Enabled = true;
}
}
//local variable for the graphics object
private Graphics G;
//creats a timer for changing the Images
Timer changeImageTimer = new Timer();
//Constructor
public Sprites(Image[] images,Graphics g)
{
//set the starting Images
Images = images;
//set the interval time to 5 seconds- in my opinion this number is rediculously high, but if that is what you want
changeImageTimer.Interval = 5000;
//Get the Tick event of the timer
changeImageTimer.Tick += new EventHandler(changeImageTimer_Tick);
//set the Graphics that you want to draw on
G = g;
}
void changeImageTimer_Tick(object sender, EventArgs e)
{
int temp=ImageIndex+1;
//if there are no more pictures to display stop the timer
if (temp > Images.Count() - 1)
{
//stop the timer
changeImageTimer.Enabled = false;
//Clear the image, replace with your own background color, or draw an image or whatever you need to do to clear the images
G.Clear(Color.White);
}
//if there are more pictures select the next picture
else
//Changeing the imageindex fires the draw method and the imageindexchanged event
ImageIndex++;
}
public void DrawExplosion(Graphics graphics,int x, int y)
{
graphics.DrawImage(DisplayedImage, x, y);
}
}
}
你認爲你的Timer事件處理程序是什麼樣的? – sq33G