图片轮播无缝版:
public partial class ImageCarouselExt : Control { #region /// <summary> /// 动画播放时间间隔定时器 /// </summary> private Timer carouselTimer = new Timer(); /// <summary> /// 轮播的三个PictureBox /// </summary> private List<PictureBox> carouselPictureBox = new List<PictureBox>() { new PictureBox(), new PictureBox(), new PictureBox() }; /// <summary> /// 轮播的三个PictureBox位置索引 /// </summary> private List<int> locationPictureBox = new List<int>() { -1, 0, 1 }; /// <summary> /// 图片轮播的时间间隔累计(-1为动画正在切换中) /// </summary> private int intervalTimeValue = 0; private int original_x = 0;// 动画对象开始制定属性原始值 private int original_y = 0;// 动画对象开始制定属性原始值 private AnimationTimer _Animation; #endregion #region private List<Image> carouselImages = new List<Image>(); /// <summary> /// 要播放的图片 /// </summary> [Description("要播放的图片")] public List<Image> CarouselImages { get { return carouselImages; } set { carouselImages = value; this.Reset(); } } private double animationTime = 500; /// <summary> /// 动画播放的总时间 /// </summary> [DefaultValue(500)] [Description("动画播放的总时间(默认500毫秒)")] public double AnimationTime { get { return animationTime; } set { animationTime = value; this.Reset(); } } private int intervalTime = 1000; /// <summary> /// 图片轮播的时间间隔 /// </summary> [DefaultValue(500)] [Description("图片轮播的时间间隔(默认1000毫秒)")] public int IntervalTime { get { return intervalTime; } set { intervalTime = value; this.Reset(); } } #endregion public ImageCarouselExt() { InitializeComponent(); this.Controls.Add(carouselPictureBox[0]); this.Controls.Add(carouselPictureBox[1]); this.Controls.Add(carouselPictureBox[2]); this.carouselTimer.Interval = 50; this.carouselTimer.Tick += new EventHandler(carouselTimer_Tick); this._Animation = new AnimationTimer(this, new AnimationOptions()); this._Animation.AnimationIng += new AnimationTimer.AnimationHandel(Animation_AnimationIng); this._Animation.AnimationEnding += new AnimationTimer.AnimationHandel(Animation_AnimationEnding); } /// <summary> /// 定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void carouselTimer_Tick(object sender, EventArgs e) { if (this.intervalTimeValue == -1) return; this.intervalTimeValue += this.carouselTimer.Interval; if (this.intervalTimeValue >= this.intervalTime) { this.intervalTimeValue = -1; this.original_x = this.carouselPictureBox[1].Location.X; this.original_y = this.carouselPictureBox[1].Location.Y; this._Animation.AT = AnimationType.EaseOut; this._Animation.Options.Transform = -this.Width; this._Animation.Options.AllTime = this.animationTime; this._Animation.Start(true, 0); } } protected void Animation_AnimationIng(object sender, AnimationEventArgs e) { Point point = new Point((int)(this.original_x + e.Transform * e.progressTime), 0);//计算动画值 this.carouselPictureBox[0].Location = new Point(point.X - this.Width, point.Y); this.carouselPictureBox[1].Location = point; this.carouselPictureBox[2].Location = new Point(point.X + this.Width, point.Y); } protected void Animation_AnimationEnding(object sender, AnimationEventArgs e) { this.carouselPictureBox[0].Location = new Point(this.Width, 0);//移动第一个PictureBox到第三 this.locationCorrect(carouselPictureBox); this.loadImageToPictureBox(); this.carouselPictureBox[0].Location = new Point(-this.Width, 0); this.carouselPictureBox[1].Location = new Point(0, 0); this.carouselPictureBox[2].Location = new Point(this.Width, 0); this.intervalTimeValue = 0; } /// <summary> /// 初始化图片 /// </summary> public void Load() { if (this.carouselImages.Count > 0) { this.locationPictureBox = new List<int>() { -1, 0, 1 }; this.resizePictureBox(); this.loadImageToPictureBox(); } }