码迷,mamicode.com
首页 > Windows程序 > 详细

图片轮播控件----------WinForm控件开发系列

时间:2019-08-17 13:02:41      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:int()   eset   csharp   timeval   timer   http   tps   下载   否则   

 技术图片  技术图片

 public partial class ImageCarouselDevelopExt : Control
  {
    #region

    /// <summary>
    /// 动画播放定时器
    /// </summary>
    private Timer carouselTimer = new Timer();

    /// <summary>
    /// 轮播的五个PictureBox
    /// </summary> 
    private List<PictureBox> carouselPictureBox = new List<PictureBox>() { new PictureBox() { SizeMode = PictureBoxSizeMode.StretchImage }, new PictureBox() { SizeMode = PictureBoxSizeMode.StretchImage }, new PictureBox() { SizeMode = PictureBoxSizeMode.StretchImage }, new PictureBox() { SizeMode = PictureBoxSizeMode.StretchImage }, new PictureBox() { SizeMode = PictureBoxSizeMode.StretchImage } };

    /// <summary>
    /// 轮播的五个PictureBox位置索引
    /// </summary>
    private List<int> carouselPictureBoxIndex = new List<int>() { 0, 1, 2, 3, 4 };

    /// <summary>
    /// 处理后的图片
    /// </summary>
    private List<Image> transformImages = new List<Image>();

    /// <summary>
    /// 图片轮播的时间间隔累计(-1为动画正在切换中)
    /// </summary>
    private int intervalTimeValue = 0;

    private AnimationTimer _Animation;

    #endregion

    #region

    private bool isReflection = false;
    /// <summary>
    /// 是否添加倒影
    /// </summary>
    [DefaultValue(false)]
    [Description("是否添加倒影")]
    public bool IsReflection
    {
      get { return this.isReflection; }
      set { this.isReflection = value; }
    }

    private List<Image> carouselImages = new List<Image>();
    /// <summary>
    /// 要播放的图片
    /// </summary>
    [Description("要播放的图片")]
    public List<Image> CarouselImages
    {
      get { return this.carouselImages; }
      set
      {
        this.carouselImages = value;
        this.Reset();
      }
    }

    private double animationTime = 300;
    /// <summary>
    /// 动画播放的总时间
    /// </summary>
    [DefaultValue(300)]
    [Description("动画播放的总时间(默认300毫秒)")]
    public double AnimationTime
    {
      get { return this.animationTime; }
      set
      {
        this.animationTime = value;
        this.Reset();
      }
    }

    private int intervalTime = 1000;
    /// <summary>
    /// 图片轮播的时间间隔
    /// </summary>
    [DefaultValue(1000)]
    [Description("图片轮播的时间间隔(默认1000毫秒)")]
    public int IntervalTime
    {
      get { return this.intervalTime; }
      set
      {
        this.intervalTime = value;
        this.Reset();
      }
    }

    #endregion

    public ImageCarouselDevelopExt()
    {
      InitializeComponent();
      this.Controls.Add(carouselPictureBox[0]);
      this.Controls.Add(carouselPictureBox[1]);
      this.Controls.Add(carouselPictureBox[2]);
      this.Controls.Add(carouselPictureBox[3]);
      this.Controls.Add(carouselPictureBox[4]);
      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;

        SizePoint _SizePoint = new SizePoint();
        _SizePoint.isswitch = false;

        _SizePoint._original_leftleft_x = leftleftPoint.X;
        _SizePoint._original_leftleft_y = leftleftPoint.Y;
        _SizePoint._transparent_leftleft_x = (rightrightPoint.X - leftleftPoint.X);
        _SizePoint._transparent_leftleft_y = (rightrightPoint.Y - leftleftPoint.Y);



        _SizePoint._original_left_width = leftSize.Width;
        _SizePoint._original_left_height = leftSize.Height;
        _SizePoint._transparent_left_width = (leftleftSize.Width - leftSize.Width);
        _SizePoint._transparent_left_height = (leftleftSize.Height - leftSize.Height);

        _SizePoint._original_left_x = leftPoint.X;
        _SizePoint._original_left_y = leftPoint.Y;
        _SizePoint._transparent_left_x = (leftleftPoint.X - leftPoint.X);
        _SizePoint._transparent_left_y = (leftleftPoint.Y - leftPoint.Y);



        _SizePoint._original_width = centerSize.Width;
        _SizePoint._original_height = centerSize.Height;
        _SizePoint._transparent_width = (leftSize.Width - centerSize.Width);
        _SizePoint._transparent_height = (leftSize.Height - centerSize.Height);

        _SizePoint._original_x = centerPoint.X;
        _SizePoint._original_y = centerPoint.Y;
        _SizePoint._transparent_x = (leftPoint.X - centerPoint.X);
        _SizePoint._transparent_y = (leftPoint.Y - centerPoint.Y);



        _SizePoint._original_right_width = rightSize.Width;
        _SizePoint._original_right_height = rightSize.Height;
        _SizePoint._transparent_right_width = (centerSize.Width - rightSize.Width);
        _SizePoint._transparent_right_height = (centerSize.Height - rightSize.Height);

        _SizePoint._original_right_x = rightPoint.X;
        _SizePoint._original_right_y = rightPoint.Y;
        _SizePoint._transparent_right_x = (centerPoint.X - rightPoint.X);
        _SizePoint._transparent_right_y = (centerPoint.Y - rightPoint.Y);



        _SizePoint._original_rightright_width = rightrightSize.Width;
        _SizePoint._original_rightright_heigh = rightrightSize.Height;
        _SizePoint._transparent_rightright_width = (rightSize.Width - rightrightSize.Width);
        _SizePoint._transparent_rightright_heigh = (rightSize.Height - rightrightSize.Height);

        _SizePoint._original_rightright_x = rightrightPoint.X;
        _SizePoint._original_rightright_y = rightrightPoint.Y;
        _SizePoint._transparent_rightright_x = (rightPoint.X - rightrightPoint.X);
        _SizePoint._transparent_rightright_y = (rightPoint.Y - rightrightPoint.Y);



        this._Animation.AT = AnimationType.EaseOut;
        this._Animation.Options.Data = _SizePoint;
        this._Animation.Options.AllTime = this.animationTime;
        this._Animation.Start(true, 0);
      }
    }

    protected void Animation_AnimationIng(object sender, AnimationEventArgs e)
    {
      SizePoint _SizePoint = (SizePoint)e.Data;

      this.carouselPictureBox[0].Location = new Point((int)(_SizePoint._original_leftleft_x + _SizePoint._transparent_leftleft_x * e.progressTime), (int)(_SizePoint._original_leftleft_y + _SizePoint._transparent_leftleft_y * e.progressTime));
      if (!_SizePoint.isswitch)
      {
        if (this.carouselPictureBox[0].Location.X > _SizePoint._original_x)
        {
          _SizePoint.isswitch = true;
          this.carouselPictureBoxIndex[0] = this.carouselPictureBoxIndex[4] + 1 >= this.transformImages.Count ? 0 : this.carouselPictureBoxIndex[4] + 1;
          this.carouselPictureBox[0].Image = this.transformImages[this.carouselPictureBoxIndex[0]];
        }
      }

      this.carouselPictureBox[1].Size = new Size((int)(_SizePoint._original_left_width + _SizePoint._transparent_left_width * e.progressTime), (int)(_SizePoint._original_left_height + _SizePoint._transparent_left_height * e.progressTime));
      this.carouselPictureBox[1].Location = new Point((int)(_SizePoint._original_left_x + _SizePoint._transparent_left_x * e.progressTime), (int)(_SizePoint._original_left_y + _SizePoint._transparent_left_y * e.progressTime));

      this.carouselPictureBox[2].Size = new Size((int)(_SizePoint._original_width + _SizePoint._transparent_width * e.progressTime), (int)(_SizePoint._original_height + _SizePoint._transparent_height * e.progressTime));
      this.carouselPictureBox[2].Location = new Point((int)(_SizePoint._original_x + _SizePoint._transparent_x * e.progressTime), (int)(_SizePoint._original_y + _SizePoint._transparent_y * e.progressTime));

      this.carouselPictureBox[3].Size = new Size((int)(_SizePoint._original_right_width + _SizePoint._transparent_right_width * e.progressTime), (int)(_SizePoint._original_right_height + _SizePoint._transparent_right_height * e.progressTime));
      this.carouselPictureBox[3].Location = new Point((int)(_SizePoint._original_right_x + _SizePoint._transparent_right_x * e.progressTime), (int)(_SizePoint._original_right_y + _SizePoint._transparent_right_y * e.progressTime));

      this.carouselPictureBox[4].Size = new Size((int)(_SizePoint._original_rightright_width + _SizePoint._transparent_rightright_width * e.progressTime), (int)(_SizePoint._original_rightright_heigh + _SizePoint._transparent_rightright_heigh * e.progressTime));
      this.carouselPictureBox[4].Location = new Point((int)(_SizePoint._original_rightright_x + _SizePoint._transparent_rightright_x * e.progressTime), (int)(_SizePoint._original_rightright_y + _SizePoint._transparent_rightright_y * e.progressTime));

    }

    protected void Animation_AnimationEnding(object sender, AnimationEventArgs e)
    {

      this.locationCorrect(carouselPictureBox);
      this.carouselPictureBox[0].BringToFront();
      this.carouselPictureBox[4].BringToFront();
      this.carouselPictureBox[1].BringToFront();
      this.carouselPictureBox[3].BringToFront();
      this.carouselPictureBox[2].BringToFront();
      this.indexCorrect(this.carouselPictureBoxIndex);

      this.intervalTimeValue = 0;
    }

    /// <summary>
    /// 初始化图片
    /// </summary>
    public void Load()
    {
      if (this.carouselImages.Count > 0)
      {
        this.carouselPictureBoxIndex = new List<int>() { 0, 1, 2, 3, 4 };
        this.resizePictureBox();
        this.loadImageToPictureBox();
        this.carouselPictureBox[0].BringToFront();
        this.carouselPictureBox[4].BringToFront();
        this.carouselPictureBox[1].BringToFront();
        this.carouselPictureBox[3].BringToFront();
        this.carouselPictureBox[2].BringToFront();
      }
    }

    /// <summary>
    /// 开始轮播图片
    /// </summary>
    public void Play()
    {
      if (!this.carouselTimer.Enabled)
      {
        this.carouselTimer.Start();
      }
    }

    /// <summary>
    /// 停止轮播图片
    /// </summary>
    public void Stop()
    {
      if (this.carouselTimer.Enabled)
      {
        this.carouselTimer.Stop();
      }
    }

    /// <summary>
    /// 刷新轮播图片
    /// </summary>
    public void Reset()
    {
      if (this.carouselTimer.Enabled)
      {
        this.carouselTimer.Stop();
        this.Load();
        this.Play();
      }
    }

    private Size centerSize;
    private Point centerPoint;

    private Size leftleftSize;
    private Point leftleftPoint;

    private Size leftSize;
    private Point leftPoint;

    private Size rightSize;
    private Point rightPoint;

    private Size rightrightSize;
    private Point rightrightPoint;

    /// <summary>
    /// 重置PictureBox大小位置
    /// </summary>
    private void resizePictureBox()
    {
      centerSize = new Size(this.Size.Width / 2, this.isReflection ? this.Size.Height / 3 * 2 : this.Size.Height / 2);
      centerPoint = new Point(centerSize.Width / 2, this.isReflection ? centerSize.Height / 3 : centerSize.Height / 2);

      leftSize = new Size(centerSize.Width / 2, centerSize.Height / 2);
      leftPoint = new Point(centerPoint.X - leftSize.Width / 2, centerPoint.Y + leftSize.Height / 2);

      leftleftSize = new Size(leftSize.Width / 2, leftSize.Height / 2);
      leftleftPoint = new Point(leftPoint.X - leftleftSize.Width / 2, leftPoint.Y + leftleftSize.Height / 2);

      rightSize = new Size(centerSize.Width / 2, centerSize.Height / 2);
      rightPoint = new Point(centerPoint.X + centerSize.Width - rightSize.Width / 2, centerPoint.Y + rightSize.Height / 2);

      rightrightSize = new Size(rightSize.Width / 2, rightSize.Height / 2);
      rightrightPoint = new Point(rightPoint.X + rightSize.Width - rightrightSize.Width / 2, rightPoint.Y + rightrightSize.Height / 2);

      this.carouselPictureBox[0].Size = leftleftSize;
      this.carouselPictureBox[0].Location = leftleftPoint;

      this.carouselPictureBox[1].Size = leftSize;
      this.carouselPictureBox[1].Location = leftPoint;

      this.carouselPictureBox[2].Size = centerSize;
      this.carouselPictureBox[2].Location = centerPoint;

      this.carouselPictureBox[3].Size = rightSize;
      this.carouselPictureBox[3].Location = rightPoint;

      this.carouselPictureBox[4].Size = rightrightSize;
      this.carouselPictureBox[4].Location = rightrightPoint;
    }

    /// <summary>
    /// PictureBox加载对应图片
    /// </summary>
    private void loadImageToPictureBox()
    {
      this.carouselPictureBoxIndex[0] = 0;
      this.carouselPictureBoxIndex[1] = this.carouselPictureBoxIndex[0] + 1 >= this.carouselImages.Count ? 0 : this.carouselPictureBoxIndex[0] + 1;
      this.carouselPictureBoxIndex[2] = this.carouselPictureBoxIndex[1] + 1 >= this.carouselImages.Count ? 0 : this.carouselPictureBoxIndex[1] + 1;
      this.carouselPictureBoxIndex[3] = this.carouselPictureBoxIndex[2] + 1 >= this.carouselImages.Count ? 0 : this.carouselPictureBoxIndex[2] + 1;
      this.carouselPictureBoxIndex[4] = this.carouselPictureBoxIndex[3] + 1 >= this.carouselImages.Count ? 0 : this.carouselPictureBoxIndex[3] + 1;

      foreach (Image image in this.carouselImages)
      {
        this.transformImages.Add(this.isReflection ? this.reflectionTransform((Bitmap)image) : image);
      }

      this.carouselPictureBox[0].Image = this.transformImages[this.carouselPictureBoxIndex[0]];
      this.carouselPictureBox[1].Image = this.transformImages[this.carouselPictureBoxIndex[1]];
      this.carouselPictureBox[2].Image = this.transformImages[this.carouselPictureBoxIndex[2]];
      this.carouselPictureBox[3].Image = this.transformImages[this.carouselPictureBoxIndex[3]];
      this.carouselPictureBox[4].Image = this.transformImages[this.carouselPictureBoxIndex[4]];
    }

    /// <summary>
    /// 纠正PictureBox位置
    /// </summary>
    /// <param name="pbList">要纠正的PictureBox</param>
    private void locationCorrect(List<PictureBox> pbList)
    {
      PictureBox pb = pbList[0];
      for (int i = 0; i < pbList.Count - 1; i++)
      {
        pbList[i] = pbList[i + 1];
      }
      pbList[pbList.Count - 1] = pb;
    }

    /// <summary>
    /// 纠正PictureBox位置索引
    /// </summary>
    /// <param name="pbList">要纠正的PictureBox</param>
    private void indexCorrect(List<int> indexList)
    {
      int index = indexList[0];
      for (int i = 0; i < indexList.Count - 1; i++)
      {
        indexList[i] = indexList[i + 1];
      }
      indexList[indexList.Count - 1] = index;
    }

    int reflectionTop = 10;//倒影边距
    int reflectionBrightness = -50;//明亮度
    int reflectionTransparentStart = 200;//倒影开始透明度
    int reflectionTransparentEnd = -0;//倒影结束透明度
    /// <summary>
    /// 倒影变换
    /// </summary>
    /// <param name="bmp"></param>
    /// <param name="val">透明度(0-255)</param>
    private Bitmap reflectionTransform(Bitmap bmp)
    {
      Color pixel;
      int reflectionHeight = bmp.Height / 2;//倒影高度
      int transparentGradient = 0;//透明梯度
      transparentGradient = (this.reflectionTransparentEnd - this.reflectionTransparentStart) / reflectionHeight;
      if (transparentGradient == 0)
        transparentGradient = 1;

      Bitmap result = new Bitmap(bmp.Width, bmp.Height + reflectionHeight + this.reflectionTop);
      for (int x = 0; x < bmp.Width; x++)
      {
        for (int y = 0; y < bmp.Height; y++)
        {
          result.SetPixel(x, y, bmp.GetPixel(x, y));
        }
      }

      for (int y = 0; y < reflectionHeight; y++)
      {
        for (int x = 0; x < bmp.Width; x++)
        {
          pixel = bmp.GetPixel(x, bmp.Height - 1 - y);
          result.SetPixel(x, bmp.Height - 1 + this.reflectionTop + y, Color.FromArgb(this.verifyRGB(this.reflectionTransparentStart + y * transparentGradient), this.verifyRGB(pixel.R + this.reflectionBrightness), this.verifyRGB(pixel.G + this.reflectionBrightness), this.verifyRGB(pixel.B + this.reflectionBrightness)));

        }
      }
      return result;
    }

    /// <summary>
    /// 检查RGB值ed有效范围
    /// </summary>
    /// <param name="rgb"></param>
    /// <returns></returns>
    private int verifyRGB(int rgb)
    {
      if (rgb < 0)
        return 0;
      if (rgb > 255)
        return 255;
      return rgb;
    }

    protected struct SizePoint
    {
      public bool isswitch;

      public int _transparent_leftleft_x;
      public int _transparent_leftleft_y;
      public int _original_leftleft_x;
      public int _original_leftleft_y;

      public int _transparent_left_width;
      public int _transparent_left_height;
      public int _transparent_left_x;
      public int _transparent_left_y;
      public int _original_left_width;
      public int _original_left_height;
      public int _original_left_x;
      public int _original_left_y;

      public int _transparent_width;
      public int _transparent_height;
      public int _transparent_x;
      public int _transparent_y;
      public int _original_width;
      public int _original_height;
      public int _original_x;
      public int _original_y;

      public int _transparent_right_width;
      public int _transparent_right_height;
      public int _transparent_right_x;
      public int _transparent_right_y;
      public int _original_right_width;
      public int _original_right_height;
      public int _original_right_x;
      public int _original_right_y;

      public int _transparent_rightright_width;
      public int _transparent_rightright_heigh;
      public int _transparent_rightright_x;
      public int _transparent_rightright_y;
      public int _original_rightright_width;
      public int _original_rightright_heigh;
      public int _original_rightright_x;
      public int _original_rightright_y;

    }

    /// <summary> 
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
        if (this._Animation != null)
        {
          this._Animation.Dispose();
        }
        if (this.carouselTimer != null)
        {
          this.carouselTimer.Dispose();
        }
      }
      base.Dispose(disposing);
    }
  }

 源码下载:CSharp图片轮播.zip

图片轮播控件----------WinForm控件开发系列

标签:int()   eset   csharp   timeval   timer   http   tps   下载   否则   

原文地址:https://www.cnblogs.com/tlmbem/p/11331388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!