C#图片轮播

本文介绍了如何使用C#实现两种图片轮播效果:无缝版和倒影版。提供了详细的源码下载链接,方便读者实践操作。
摘要由CSDN通过智能技术生成

       

     

图片轮播无缝版:

  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();
      }
    }

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值