Advertisement
Yassine_Abbani

Slider [Image],[Costume Control]

May 11th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.01 KB | None | 0 0
  1. #region Import
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. #endregion
  9. #region CopyRight
  10. // Creator : Yassine Abbani
  11. // pastebin : https://pastebin.com/u/Yassine_Abbani
  12. // favebook : https://www.facebook.com/yassineabbani.user
  13. #endregion
  14.  
  15. class ImageSlider : Panel
  16. {
  17.     Timer _timer;
  18.     int _captionTextLeft = 20;
  19.     int _captionPosX = 20;
  20.     int _pageIndex = 0;
  21.  
  22.     protected List<Image> _imageList = new List<Image>();
  23.     protected List<string> _captionList = new List<string>();
  24.     protected List<Color> _captionBgColor = new List<Color>();
  25.  
  26.     xButton leftButton;
  27.     xButton rightButton;
  28.  
  29.     public ImageSlider()
  30.     {
  31.         this.Animation = true;
  32.         this.CaptionAnimationSpeed = 50;
  33.         this.CaptionTextLeft = 20;
  34.         this.CaptionHeight = 50;
  35.         this.CaptionBackgrounColor = Color.Black;
  36.         this.CaptionOpacity = 100;
  37.  
  38.         leftButton = new xButton();
  39.         leftButton.Text = "<";
  40.         leftButton.Click += new EventHandler(leftButton_Click);
  41.  
  42.         rightButton = new xButton();
  43.         rightButton.Text = ">";
  44.         rightButton.Click += new EventHandler(rightButton_Click);
  45.  
  46.         this.Resize += ImageSlider_Resize;
  47.  
  48.         this.Controls.Add(leftButton);
  49.         this.Controls.Add(rightButton);
  50.     }
  51.  
  52.     void ImageSlider_Resize(object sender, EventArgs e)
  53.     {
  54.         leftButton.Location = new Point(0, (this.Height / 2) - (leftButton.Height / 2));
  55.         rightButton.Location = new Point(this.Width - rightButton.Width, (this.Height / 2) - (rightButton.Height / 2));
  56.     }
  57.  
  58.     void leftButton_Click(object sender, EventArgs e)
  59.     {
  60.  
  61.         if (_pageIndex > 0)
  62.         {
  63.             --_pageIndex;
  64.         }
  65.         else
  66.         {
  67.             _pageIndex = _imageList.Count - 1;
  68.         }
  69.  
  70.         if (Animation)
  71.         {
  72.             _captionPosX = this.Width;
  73.             this.DoubleBuffered = true;
  74.  
  75.             _timer = new Timer();
  76.             _timer.Interval = 1;
  77.             _timer.Tick += new EventHandler(_timer_Tick);
  78.             _timer.Start();
  79.         }
  80.         else
  81.         {
  82.             _captionPosX = _captionTextLeft;
  83.             this.Invalidate();
  84.         }
  85.     }
  86.  
  87.     void rightButton_Click(object sender, EventArgs e)
  88.     {
  89.  
  90.         if (_pageIndex < _imageList.Count - 1)
  91.         {
  92.             ++_pageIndex;
  93.         }
  94.         else
  95.         {
  96.             _pageIndex = 0;
  97.         }
  98.  
  99.         if (Animation)
  100.         {
  101.             _captionPosX = this.Width;
  102.             DoubleBuffered = true;
  103.  
  104.             _timer = new Timer();
  105.             _timer.Interval = 1;
  106.             _timer.Tick += new EventHandler(_timer_Tick);
  107.             _timer.Start();
  108.         }
  109.         else
  110.         {
  111.             _captionPosX = _captionTextLeft;
  112.             this.Invalidate();
  113.         }
  114.     }
  115.  
  116.     void _timer_Tick(object sender, EventArgs e)
  117.     {
  118.         if (_captionPosX >= _captionTextLeft)
  119.         {
  120.             int subtract = CaptionAnimationSpeed;
  121.  
  122.             int diff = _captionPosX - subtract;
  123.  
  124.             if (diff < subtract)
  125.             {
  126.                 _captionPosX -= _captionPosX - _captionTextLeft;
  127.             }
  128.             else
  129.             {
  130.                 _captionPosX -= subtract;
  131.             }
  132.  
  133.             this.Invalidate();
  134.         }
  135.         else
  136.         {
  137.             this.DoubleBuffered = false;
  138.             _timer.Dispose();
  139.         }
  140.     }
  141.  
  142.     public void AddImage(string path)
  143.     {
  144.         Image img = Image.FromFile(path);
  145.         _AddImage(img, "", this.CaptionBackgrounColor);
  146.     }
  147.  
  148.     public void AddImage(string path, string caption)
  149.     {
  150.         Image img = Image.FromFile(path);
  151.         _AddImage(img, caption, this.CaptionBackgrounColor);
  152.     }
  153.  
  154.     public void AddImage(string path, string caption, Color captionBackgroundColor)
  155.     {
  156.         Image img = Image.FromFile(path);
  157.         _AddImage(img, caption, captionBackgroundColor);
  158.     }
  159.  
  160.     public void AddImage(Image img)
  161.     {
  162.         _AddImage(img, "", this.CaptionBackgrounColor);
  163.     }
  164.  
  165.     public void AddImage(Image img, string caption)
  166.     {
  167.         _AddImage(img, caption, this.CaptionBackgrounColor);
  168.     }
  169.  
  170.     public void AddImage(Image img, string caption, Color captionBackgroundColor)
  171.     {
  172.         _AddImage(img, caption, captionBackgroundColor);
  173.     }
  174.  
  175.     protected void _AddImage(Image img, string caption, Color captionBackgroundColor)
  176.     {
  177.         _imageList.Add(img);
  178.         _captionList.Add(caption);
  179.         _captionBgColor.Add(captionBackgroundColor);
  180.     }
  181.  
  182.     public int CaptionHeight
  183.     {
  184.         set;
  185.         get;
  186.     }
  187.  
  188.     public int CaptionTextLeft
  189.     {
  190.         set
  191.         {
  192.             _captionPosX = value;
  193.             _captionTextLeft = value;
  194.         }
  195.         get
  196.         {
  197.             return _captionTextLeft;
  198.         }
  199.     }
  200.  
  201.     public Color CaptionBackgrounColor
  202.     {
  203.         set;
  204.         get;
  205.     }
  206.  
  207.     public int CaptionOpacity
  208.     {
  209.         set;
  210.         get;
  211.     }
  212.  
  213.     public int CaptionAnimationSpeed
  214.     {
  215.         set;
  216.         get;
  217.     }
  218.  
  219.     public bool Animation
  220.     {
  221.         set;
  222.         get;
  223.     }
  224.  
  225.     public xButton LeftButton
  226.     {
  227.         get
  228.         {
  229.             return leftButton;
  230.         }
  231.     }
  232.  
  233.     public xButton RightButton
  234.     {
  235.         get
  236.         {
  237.             return rightButton;
  238.         }
  239.     }
  240.  
  241.     protected override void OnPaint(PaintEventArgs e)
  242.     {
  243.         base.OnPaint(e);
  244.  
  245.         Graphics g = e.Graphics;
  246.         try
  247.         {
  248.             Color captionBgColor = Color.FromArgb(CaptionOpacity, _captionBgColor[_pageIndex].R, _captionBgColor[_pageIndex].G, _captionBgColor[_pageIndex].B);
  249.             g.DrawImage(_imageList[_pageIndex], new Rectangle(0, 0, this.Width, this.Height));
  250.             g.FillRectangle(new SolidBrush(captionBgColor), new Rectangle(0, this.Height - this.CaptionHeight, this.Width, this.Height));
  251.  
  252.             string caption = _captionList[_pageIndex];
  253.  
  254.             SizeF fontSize = g.MeasureString(_captionList[_pageIndex], this.Font);
  255.             g.DrawString(_captionList[_pageIndex], this.Font, new SolidBrush(this.ForeColor), _captionPosX, this.Height - (int)(this.CaptionHeight - (fontSize.Height / 2)));
  256.         }
  257.         catch { }
  258.     }
  259.  
  260.     public class xButton : Button
  261.     {
  262.  
  263.         public xButton()
  264.         {
  265.             this.BackColor = Color.Black;
  266.             this.Height = 50;
  267.             this.Width = 50;
  268.         }
  269.  
  270.         protected override void OnPaint(PaintEventArgs pevent)
  271.         {
  272.             Graphics g = pevent.Graphics;
  273.             Rectangle area = new Rectangle(0, 0, this.Width, this.Height);
  274.  
  275.             g.FillRectangle(new SolidBrush(this.BackColor), area);
  276.             SizeF fontSize = g.MeasureString(this.Text, this.Font);
  277.             g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), (this.Width - fontSize.Width) / 2, (this.Height - fontSize.Height) / 2);
  278.         }
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement