Advertisement
Yassine_Abbani

Circular [Progress Bar], [Custom Control]

May 8th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.46 KB | None | 0 0
  1. #region Derectives
  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.Text;
  10. using System.Windows.Forms;
  11. #endregion
  12. #region Browse
  13. /* Copyright & Contact
  14. * --------------------------------------------------------------------------------<
  15. * Tool Name    : ProgressBar                                                     *
  16. * From Project : Creator Eye                                                      *
  17. * Project Lang : C#                                                               *
  18. * Creator      : Yassine Abbani                                                   *
  19. * Facebook     : https://www.facebook.com/YassineAbbani.user                      *
  20. * Pastebin     : https://pastebin.com/u/Yassine_Abbani                            *
  21. * Youtube      : https://www.youtube.com/channel/UCqvOCRs3HWbPH4yuZuTx8mw         *
  22. * Version      : 1.0 Beta                                                         *
  23. * Color        :  Dark Theme                                                      *
  24. * Style        :  Circular                                                         *
  25. *>--------------------------------------------------------------------------------<
  26. */
  27. /* Features
  28. * ------------------------
  29. *   Custom Properties:
  30.  *   Create Circular Style.
  31.  *   add the range specified by minimum and maximum value.
  32.  *   Using  Relative Value Style
  33. */
  34. /*  history
  35. * ------------------------
  36. * 1.0 (20 Feb 2018):
  37.  * In my Source Code Get Circular progress-bar Like Gmail Style with Great Effect.
  38.  * So, in this case,you can use this controller as Circular Progress Bar with input Or edit value, and maximum value.
  39. *
  40. */
  41. #endregion
  42. #region CircularProgressBar
  43.  
  44. public class CeCircularProgressBar : Control
  45. {
  46.  
  47.     #region Enums
  48.  
  49.     public enum _ProgressShape
  50.     {
  51.         Round,
  52.         Flat
  53.     }
  54.  
  55.     #endregion
  56.     #region Variables
  57.  
  58.     private long _Value;
  59.     private long _Maximum = 100;
  60.     private Color _ProgressColor1 = Color.FromArgb(92, 92, 92);
  61.     private Color _ProgressColor2 = Color.FromArgb(92, 92, 92);
  62.     private _ProgressShape ProgressShapeVal;
  63.  
  64.     #endregion
  65.     #region Custom Properties
  66.  
  67.     public long Value
  68.     {
  69.         get { return _Value; }
  70.         set
  71.         {
  72.             if (value > _Maximum)
  73.                 value = _Maximum;
  74.             _Value = value;
  75.             Invalidate();
  76.         }
  77.     }
  78.  
  79.     public long Maximum
  80.     {
  81.         get { return _Maximum; }
  82.         set
  83.         {
  84.             if (value < 1)
  85.                 value = 1;
  86.             _Maximum = value;
  87.             Invalidate();
  88.         }
  89.     }
  90.  
  91.     public Color ProgressColor1
  92.     {
  93.         get { return _ProgressColor1; }
  94.         set
  95.         {
  96.             _ProgressColor1 = value;
  97.             Invalidate();
  98.         }
  99.     }
  100.  
  101.     public Color ProgressColor2
  102.     {
  103.         get { return _ProgressColor2; }
  104.         set
  105.         {
  106.             _ProgressColor2 = value;
  107.             Invalidate();
  108.         }
  109.     }
  110.  
  111.     public _ProgressShape ProgressShape
  112.     {
  113.         get { return ProgressShapeVal; }
  114.         set
  115.         {
  116.             ProgressShapeVal = value;
  117.             Invalidate();
  118.         }
  119.     }
  120.  
  121.     #endregion
  122.     #region EventArgs
  123.  
  124.     protected override void OnResize(EventArgs e)
  125.     {
  126.         base.OnResize(e);
  127.         SetStandardSize();
  128.     }
  129.  
  130.     protected override void OnSizeChanged(EventArgs e)
  131.     {
  132.         base.OnSizeChanged(e);
  133.         SetStandardSize();
  134.     }
  135.  
  136.     protected override void OnPaintBackground(PaintEventArgs p)
  137.     {
  138.         base.OnPaintBackground(p);
  139.     }
  140.  
  141.     #endregion
  142.  
  143.     public CeCircularProgressBar()
  144.     {
  145.         Size = new Size(130, 130);
  146.         Font = new Font("Segoe UI", 15);
  147.         MinimumSize = new Size(100, 100);
  148.         DoubleBuffered = true;
  149.     }
  150.  
  151.     private void SetStandardSize()
  152.     {
  153.         int _Size = Math.Max(Width, Height);
  154.         Size = new Size(_Size, _Size);
  155.     }
  156.  
  157.     public void Increment(int Val)
  158.     {
  159.         this._Value += Val;
  160.         Invalidate();
  161.     }
  162.  
  163.     public void Decrement(int Val)
  164.     {
  165.         this._Value -= Val;
  166.         Invalidate();
  167.     }
  168.  
  169.     protected override void OnPaint(PaintEventArgs e)
  170.     {
  171.         base.OnPaint(e);
  172.         using (Bitmap bitmap = new Bitmap(this.Width, this.Height))
  173.         {
  174.             using (Graphics graphics = Graphics.FromImage(bitmap))
  175.             {
  176.                 graphics.SmoothingMode = SmoothingMode.AntiAlias;
  177.                 graphics.Clear(this.BackColor);
  178.                 using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, this._ProgressColor1, this._ProgressColor2, LinearGradientMode.ForwardDiagonal))
  179.                 {
  180.                     using (Pen pen = new Pen(brush, 14f))
  181.                     {
  182.                         switch (this.ProgressShapeVal)
  183.                         {
  184.                             case _ProgressShape.Round:
  185.                                 pen.StartCap = LineCap.Round;
  186.                                 pen.EndCap = LineCap.Round;
  187.                                 break;
  188.  
  189.                             case _ProgressShape.Flat:
  190.                                 pen.StartCap = LineCap.Flat;
  191.                                 pen.EndCap = LineCap.Flat;
  192.                                 break;
  193.                         }
  194.                         graphics.DrawArc(pen, 0x12, 0x12, (this.Width - 0x23) - 2, (this.Height - 0x23) - 2, -90, (int)Math.Round((double)((360.0 / ((double)this._Maximum)) * this._Value)));
  195.                     }
  196.                 }
  197.                 using (LinearGradientBrush brush2 = new LinearGradientBrush(this.ClientRectangle, Color.FromArgb(0x34, 0x34, 0x34), Color.FromArgb(0x34, 0x34, 0x34), LinearGradientMode.Vertical))
  198.                 {
  199.                     graphics.FillEllipse(brush2, 0x18, 0x18, (this.Width - 0x30) - 1, (this.Height - 0x30) - 1);
  200.                 }
  201.                 SizeF MS = graphics.MeasureString(Convert.ToString(Convert.ToInt32((100 / _Maximum) * _Value)), Font);
  202.                 graphics.DrawString(Convert.ToString(Convert.ToInt32((100 / _Maximum) * _Value)), Font, Brushes.White, Convert.ToInt32(Width / 2 - MS.Width / 2), Convert.ToInt32(Height / 2 - MS.Height / 2));
  203.                 e.Graphics.DrawImage(bitmap, 0, 0);
  204.                 graphics.Dispose();
  205.                 bitmap.Dispose();
  206.             }
  207.         }
  208.     }
  209. }
  210.  
  211. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement