Advertisement
Yassine_Abbani

Indicator [Progress Bar], [Custom Control]

May 11th, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.77 KB | None | 0 0
  1. #region Directives
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Drawing.Text;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using System.IO;
  13. using System.Drawing.Imaging;
  14. using System.Linq;
  15. #endregion
  16. #region Browse
  17. /* Copyright & Contact
  18. * --------------------------------------------------------------------------------<
  19. * Tool Name    : ProgressBar                                                     *
  20. * From Project : Creator Eye                                                      *
  21. * Project Lang : C#                                                               *
  22. * Creator      : Yassine Abbani                                                   *
  23. * Facebook     : https://www.facebook.com/YassineAbbani.user                      *
  24. * Pastebin     : https://pastebin.com/u/Yassine_Abbani                            *
  25. * Youtube      : https://www.youtube.com/channel/UCqvOCRs3HWbPH4yuZuTx8mw         *
  26. * Version      : 1.0 Beta                                                         *
  27. * Color        :  Dark Theme                                                      *
  28. * Style        :  Indicator                                                         *
  29. *>--------------------------------------------------------------------------------<
  30. */
  31. /* Features
  32. * ------------------------
  33. *   Custom Properties:
  34.  *   Create Indicator Style.
  35.  *   add the range specified by minimum and maximum value.
  36.  *   Using  Relative Value Style
  37. */
  38. /*  history
  39. * ------------------------
  40. * 1.0 (20 Feb 2018):
  41.  * In my Source Code Get Circular progress-bar Like Gmail Style with Great Effect.
  42.  * So, in this case,you can use this controller as Circular Progress Bar with input Or edit value, and maximum value.
  43. *
  44. */
  45. #endregion
  46. #region  Progress Indicator
  47.  
  48. class CeIndicatorProgressBar : Control
  49. {
  50.  
  51.     #region Variables
  52.  
  53.     private readonly SolidBrush BaseColor = new SolidBrush(Color.FromArgb(76, 76, 76));
  54.     private readonly SolidBrush AnimationColor = new SolidBrush(Color.Gray);
  55.  
  56.     private readonly Timer AnimationSpeed = new Timer();
  57.     private PointF[] FloatPoint;
  58.     private BufferedGraphics BuffGraphics;
  59.     private int IndicatorIndex;
  60.     private readonly BufferedGraphicsContext GraphicsContext = BufferedGraphicsManager.Current;
  61.  
  62.     #endregion
  63.     #region Custom Properties
  64.  
  65.     public Color P_BaseColor
  66.     {
  67.         get { return BaseColor.Color; }
  68.         set { BaseColor.Color = value; }
  69.     }
  70.  
  71.     public Color P_AnimationColor
  72.     {
  73.         get { return AnimationColor.Color; }
  74.         set { AnimationColor.Color = value; }
  75.     }
  76.  
  77.     public int P_AnimationSpeed
  78.     {
  79.         get { return AnimationSpeed.Interval; }
  80.         set { AnimationSpeed.Interval = value; }
  81.     }
  82.  
  83.     #endregion
  84.     #region EventArgs
  85.  
  86.     protected override void OnSizeChanged(EventArgs e)
  87.     {
  88.         base.OnSizeChanged(e);
  89.         SetStandardSize();
  90.         UpdateGraphics();
  91.         SetPoints();
  92.     }
  93.  
  94.     protected override void OnEnabledChanged(EventArgs e)
  95.     {
  96.         base.OnEnabledChanged(e);
  97.         AnimationSpeed.Enabled = this.Enabled;
  98.     }
  99.  
  100.     protected override void OnHandleCreated(EventArgs e)
  101.     {
  102.         base.OnHandleCreated(e);
  103.         AnimationSpeed.Tick += AnimationSpeed_Tick;
  104.         AnimationSpeed.Start();
  105.     }
  106.  
  107.     private void AnimationSpeed_Tick(object sender, EventArgs e)
  108.     {
  109.         if (IndicatorIndex.Equals(0))
  110.         {
  111.             IndicatorIndex = FloatPoint.Length - 1;
  112.         }
  113.         else
  114.         {
  115.             IndicatorIndex -= 1;
  116.         }
  117.         this.Invalidate(false);
  118.     }
  119.  
  120.     #endregion
  121.  
  122.     public CeIndicatorProgressBar()
  123.     {
  124.         this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
  125.  
  126.         Size = new Size(80, 80);
  127.         Text = string.Empty;
  128.         MinimumSize = new Size(80, 80);
  129.         SetPoints();
  130.         AnimationSpeed.Interval = 100;
  131.     }
  132.  
  133.     private void SetStandardSize()
  134.     {
  135.         int _Size = Math.Max(Width, Height);
  136.         Size = new Size(_Size, _Size);
  137.     }
  138.  
  139.     private void SetPoints()
  140.     {
  141.         Stack<PointF> stack = new Stack<PointF>();
  142.         PointF startingFloatPoint = new PointF(((float)this.Width) / 2f, ((float)this.Height) / 2f);
  143.         for (float i = 0f; i < 360f; i += 45f)
  144.         {
  145.             this.SetValue(startingFloatPoint, (int)Math.Round((double)((((double)this.Width) / 2.0) - 15.0)), (double)i);
  146.             PointF endPoint = this.EndPoint;
  147.             endPoint = new PointF(endPoint.X - 7.5f, endPoint.Y - 7.5f);
  148.             stack.Push(endPoint);
  149.         }
  150.         this.FloatPoint = stack.ToArray();
  151.     }
  152.  
  153.     private void UpdateGraphics()
  154.     {
  155.         if ((this.Width > 0) && (this.Height > 0))
  156.         {
  157.             Size size2 = new Size(this.Width + 1, this.Height + 1);
  158.             this.GraphicsContext.MaximumBuffer = size2;
  159.             this.BuffGraphics = this.GraphicsContext.Allocate(this.CreateGraphics(), this.ClientRectangle);
  160.             this.BuffGraphics.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  161.         }
  162.     }
  163.  
  164.     protected override void OnPaint(PaintEventArgs e)
  165.     {
  166.         base.OnPaint(e);
  167.         this.BuffGraphics.Graphics.Clear(this.BackColor);
  168.         int num2 = this.FloatPoint.Length - 1;
  169.         for (int i = 0; i <= num2; i++)
  170.         {
  171.             if (this.IndicatorIndex == i)
  172.             {
  173.                 this.BuffGraphics.Graphics.FillEllipse(this.AnimationColor, this.FloatPoint[i].X, this.FloatPoint[i].Y, 15f, 15f);
  174.             }
  175.             else
  176.             {
  177.                 this.BuffGraphics.Graphics.FillEllipse(this.BaseColor, this.FloatPoint[i].X, this.FloatPoint[i].Y, 15f, 15f);
  178.             }
  179.         }
  180.         this.BuffGraphics.Render(e.Graphics);
  181.     }
  182.  
  183.  
  184.     private double Rise;
  185.     private double Run;
  186.     private PointF _StartingFloatPoint;
  187.  
  188.     private X AssignValues<X>(ref X Run, X Length)
  189.     {
  190.         Run = Length;
  191.         return Length;
  192.     }
  193.  
  194.     private void SetValue(PointF StartingFloatPoint, int Length, double Angle)
  195.     {
  196.         double CircleRadian = Math.PI * Angle / 180.0;
  197.  
  198.         _StartingFloatPoint = StartingFloatPoint;
  199.         Rise = AssignValues(ref Run, Length);
  200.         Rise = Math.Sin(CircleRadian) * Rise;
  201.         Run = Math.Cos(CircleRadian) * Run;
  202.     }
  203.  
  204.     private PointF EndPoint
  205.     {
  206.         get
  207.         {
  208.             float LocationX = Convert.ToSingle(_StartingFloatPoint.Y + Rise);
  209.             float LocationY = Convert.ToSingle(_StartingFloatPoint.X + Run);
  210.  
  211.             return new PointF(LocationY, LocationX);
  212.         }
  213.     }
  214. }
  215.  
  216. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement