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

  

 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

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

时间: 2024-11-07 02:24:14

图片轮播控件----------WinForm控件开发系列的相关文章

Android学习笔记之图片轮播...

PS:一个bug又折腾了一个下午....哎... 学习内容: 1.Android利用ViewPager和PagerAdapter实现图片轮播... 2.使用反射机制获取Android的资源信息...     图片轮播是非常常见的一种动画效果,在app中也是很常用的一个效果,这里就简单的来实现一下这个功能,Android中想要实现图片轮播,需要使用到ViewPager这个控件来实现,这个控件的主要功能是实现图片的滑动效果...那么有了滑动,在滑动的基础上附上图片也就实现了图片轮播的效果...这个控

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

/// <summary> /// 走马灯图片轮播控件 /// </summary> [ToolboxItem(true)] [DefaultProperty("Images")] [Description("走马灯图片轮播控件")] public partial class ImageCarouselExt : Control { #region private bool barShow = true; /// <summary>

Android高级图片滚动控件,编写3D版的图片轮播器

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17482089 大家好,好久不见了,最近由于工作特别繁忙,已经有一个多月的时间没写博客了,我也是深感惭愧.那么今天的这篇既然是阔别了一个多月的文章,当然要带来更加给力点的内容了,那么话不多说,赶快进入到今天的正题吧. 说 到图片轮播器,很多的Android应用中都会带有这个功能,比如说网易新闻.淘宝等.最新我们公司的一款应用也加入了这个功能,并且在图片轮播的基础上 还增加了三维立体

【转】 iOS开发UI篇—UIScrollView控件实现图片轮播

原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: 1 #import "YYViewController.h" 2 3 @interface YYViewController () <UIScrollViewDelegate> 4 @property (w

Swift基础 - - StoryBoard间切换与UIScrollView控件实现图片轮播

界面切换 在项目中可以把耦合度比较高的界面放在通过一个StoryBoard中,可以按照功能使用多个StoryBoard搭建界面,这样便于项目维护以及多人开发,对于多个StoryBoard间切换,可以使用以下代码: @IBAction func ChangeOne(sender: UIButton) { var oneStoryBoard:UIStoryboard = UIStoryboard(name: "One", bundle: NSBundle.mainBundle()) let

UISCrollView —— 图片轮播器封装实现(三)——(第三讲)

1.分析 利用xib布局,然后自定义一个UIView,解析xib,然后利用控制器传入数据,将其加载到控制器的view上展示即可 2. 程序结构 3. 代码具体实现 1>  xib文件 2>  创建类 XMGPageView,然后将其与xib文件关联,选中xib文件,然后设置下文中 " custom class " 为定义的类,让其来管理xib 1>    (如图,设置xib与类关联) 2>  XMGPageView.h 文件中,声明分页图片数组属性,及其一个快速

UIScrollView实现图片轮播器及其无限循环效果

图片轮播器: 一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: 1 #import "YYViewController.h" 2 3 @interface YYViewController () <UIScrollViewDelegate> 4 @property (weak, nonatomic) IBOutlet UIScrollView *scrollview; 5 /** 6 * 页码 7 */ 8 @prop

UISCrollView —— 图片轮播器实现(三)——(第二讲)

1. 所用知识点 1> UIScrollView的基本属性,和UIPageControl设置,还有就是要用到NSTimer来定时实现UIScrollView的图片轮播 2> NSTimer简单介绍: 2.1  NSTimer叫做“定时器”,它的作用如下 * 在指定的时间执行指定的任务 * 每隔一段时间执行指定的任务 2.2  NSTimer简单使用: 1> 调用下面的方法就会开启一个定时任务 + (NSTimer *)scheduledTimerWithTimeInterval:(NST

Android事件分发学习应用-图片轮播实现

前一篇写到Android事件分发机制学习笔记,下面我们通过一个实例的应用来实践理解下Android事件分发的机制.我们这里来实现一个图片的轮播功能,最后顺便实现下图片的自动轮播. 我们的图片轮播是封装在一个ViewGroup里,当我们进行横向滑动的时候,我们需要阻止事件从ViewGroup往子控件分发,ViewGroup来消费我们当前的滑动图片何去何从.下面我们贴出我们的封装的ViewGroup的代码实现如下: public class ImageSwitcher extends ViewGro