Advertisement
Yassine_Abbani

IOS Button [Custom Control]

May 22nd, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.34 KB | None | 0 0
  1. #region Directives
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8. using System.Drawing;
  9. using System.ComponentModel;
  10.  
  11. #endregion
  12. #region Browse
  13. /* Copyright & Contact
  14. * --------------------------------------------------------------------------------<
  15. * Tool Name    : IOS Button                                                       *
  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.3 Beta                                                         *
  23. * Color        : MultiColor +                                                     *
  24. * Style        : IOS                                                             *
  25. *>--------------------------------------------------------------------------------<
  26. */
  27. /* Features
  28. * ------------------------
  29. * Add Switch Button Properties
  30. * Checked   > Gets or sets the switch control color while Checked
  31. * Checked   > Gets or sets the switch control ball color while checked
  32. * Unchecked > Gets or sets the switch control color while Checked
  33. * Unchecked > Gets or sets the switch control ball color while checked
  34. * Switched  > Gets or set Cheked or Uncheked Button
  35. */
  36. /*  history
  37. * ------------------------
  38. * 1.0 (6 Jan 2018):
  39. * Get Custom Control IOS button  by Creator Eye development in c# Winform Project
  40. * Fixed all bugs
  41. */
  42. #endregion
  43. #region Helper Methods
  44.  
  45. public class HelperMethods
  46. {
  47.  
  48.     public GraphicsPath GP = null;
  49.  
  50.     public enum MouseMode
  51.     {
  52.         NormalMode,
  53.         Hovered,
  54.         Pushed
  55.     };
  56.  
  57.     public void DrawImageFromBase64(Graphics G, string Base64Image, Rectangle Rect)
  58.     {
  59.         Image IM = null;
  60.         using (System.IO.MemoryStream ms = new System.IO.MemoryStream(Convert.FromBase64String(Base64Image)))
  61.         {
  62.             IM = System.Drawing.Image.FromStream(ms);
  63.         }
  64.         G.DrawImage(IM, Rect);
  65.     }
  66.  
  67.     public GraphicsPath RoundRec(Rectangle r, int Curve, bool TopLeft = true, bool TopRight = true, bool BottomLeft = true, bool BottomRight = true)
  68.     {
  69.         GraphicsPath CreateRoundPath = new GraphicsPath(FillMode.Winding);
  70.         if (TopLeft)
  71.         {
  72.             CreateRoundPath.AddArc(r.X, r.Y, Curve, Curve, 180f, 90f);
  73.         }
  74.         else
  75.         {
  76.             CreateRoundPath.AddLine(r.X, r.Y, r.X, r.Y);
  77.         }
  78.         if (TopRight)
  79.         {
  80.             CreateRoundPath.AddArc(r.Right - Curve, r.Y, Curve, Curve, 270f, 90f);
  81.         }
  82.         else
  83.         {
  84.             CreateRoundPath.AddLine(r.Right - r.Width, r.Y, r.Width, r.Y);
  85.         }
  86.         if (BottomRight)
  87.         {
  88.             CreateRoundPath.AddArc(r.Right - Curve, r.Bottom - Curve, Curve, Curve, 0f, 90f);
  89.         }
  90.         else
  91.         {
  92.             CreateRoundPath.AddLine(r.Right, r.Bottom, r.Right, r.Bottom);
  93.  
  94.         }
  95.         if (BottomLeft)
  96.         {
  97.             CreateRoundPath.AddArc(r.X, r.Bottom - Curve, Curve, Curve, 90f, 90f);
  98.         }
  99.         else
  100.         {
  101.             CreateRoundPath.AddLine(r.X, r.Bottom, r.X, r.Bottom);
  102.         }
  103.         CreateRoundPath.CloseFigure();
  104.         return CreateRoundPath;
  105.     }
  106.  
  107.     public void FillRoundedPath(Graphics G, Color C, Rectangle Rect, int Curve, bool TopLeft = true, bool TopRight = true, bool BottomLeft = true, bool BottomRight = true)
  108.     {
  109.         G.FillPath(new SolidBrush(C), RoundRec(Rect, Curve, TopLeft, TopRight, BottomLeft, BottomRight));
  110.     }
  111.  
  112.     public void FillRoundedPath(Graphics G, Brush B, Rectangle Rect, int Curve, bool TopLeft = true, bool TopRight = true, bool BottomLeft = true, bool BottomRight = true)
  113.     {
  114.         G.FillPath(B, RoundRec(Rect, Curve, TopLeft, TopRight, BottomLeft, BottomRight));
  115.     }
  116.  
  117.     public void DrawRoundedPath(Graphics G, Color C, Single Size, Rectangle Rect, int Curve, bool TopLeft = true, bool TopRight = true, bool BottomLeft = true, bool BottomRight = true)
  118.     {
  119.         G.DrawPath(new Pen(C, Size), RoundRec(Rect, Curve, TopLeft, TopRight, BottomLeft, BottomRight));
  120.     }
  121.  
  122.     public void DrawTriangle(Graphics G, Color C, Single Size, Point P1_0, Point P1_1, Point P2_0, Point P2_1, Point P3_0, Point P3_1)
  123.     {
  124.  
  125.         G.DrawLine(new Pen(C, Size), P1_0, P1_1);
  126.         G.DrawLine(new Pen(C, Size), P2_0, P2_1);
  127.         G.DrawLine(new Pen(C, Size), P3_0, P3_1);
  128.  
  129.     }
  130.  
  131.     public Pen PenRGBColor(int R, int G, int B, Single size)
  132.     { return new Pen(System.Drawing.Color.FromArgb(R, G, B), size); }
  133.  
  134.     public Pen PenHTMlColor(String C_WithoutHash, float Thick)
  135.     { return new Pen(GetHTMLColor(C_WithoutHash), Thick); }
  136.  
  137.     public SolidBrush SolidBrushRGBColor(int R, int G, int B, int A = 0)
  138.     { return new SolidBrush(System.Drawing.Color.FromArgb(A, R, G, B)); }
  139.  
  140.     public SolidBrush SolidBrushHTMlColor(String C_WithoutHash)
  141.     { return new SolidBrush(GetHTMLColor(C_WithoutHash)); }
  142.  
  143.     public Color GetHTMLColor(String C_WithoutHash)
  144.     { return ColorTranslator.FromHtml("#" + C_WithoutHash); }
  145.  
  146.     public String ColorToHTML(Color C)
  147.     { return ColorTranslator.ToHtml(C); }
  148.  
  149.     public Color SetARGB(int A, int R, int G, int B)
  150.     { return System.Drawing.Color.FromArgb(A, R, G, B); }
  151.  
  152.     public Color SetRGB(int R, int G, int B)
  153.     { return System.Drawing.Color.FromArgb(R, G, B); }
  154.  
  155.     public void CentreString(Graphics G, String Text, Font font, Brush brush, Rectangle Rect)
  156.     { G.DrawString(Text, font, brush, new Rectangle(Rect.X, Rect.Y, Rect.Width, Rect.Height), new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }); }
  157.  
  158.     public void LeftString(Graphics G, String Text, Font font, Brush brush, Rectangle Rect)
  159.     {
  160.         G.DrawString(Text, font, brush, new Rectangle(4, Rect.Y + Convert.ToInt32(Rect.Height / 2) - Convert.ToInt32(G.MeasureString(Text, font).Height / 2) + 0, Rect.Width, Rect.Height), new StringFormat { Alignment = StringAlignment.Near });
  161.     }
  162.  
  163.     public void RightString(Graphics G, string Text, Font font, Brush brush, Rectangle Rect)
  164.     {
  165.         G.DrawString(Text, font, brush, new Rectangle(4, Convert.ToInt32(Rect.Y + (Rect.Height / 2) - (G.MeasureString(Text, font).Height / 2)), Rect.Width - Rect.Height + 10, Rect.Height), new StringFormat { Alignment = StringAlignment.Far });
  166.     }
  167. }
  168.  
  169. #endregion
  170. #region Switch
  171. [DefaultEvent("Switch")]
  172. public class Ce_IOS_SwitchButton : Control
  173. {
  174.  
  175.     #region Variables
  176.  
  177.     private bool _Switched = false;
  178.     private static HelperMethods H = new HelperMethods();
  179.     private Color _UnCheckedColor = H.GetHTMLColor("dedede");
  180.     private Color _CheckedColor = H.GetHTMLColor("4682B4");
  181.     private Color _CheckedBallColor = Color.White;
  182.     private Color _UnCheckedBallColor = Color.White;
  183.  
  184.     #endregion
  185.  
  186.     #region Constructors
  187.  
  188.     public Ce_IOS_SwitchButton()
  189.     {
  190.         SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
  191.         ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
  192.         DoubleBuffered = true;
  193.         UpdateStyles();
  194.         BackColor = Color.Transparent;
  195.         Cursor = Cursors.Hand;
  196.         Size = new Size(30, 19); ;
  197.     }
  198.  
  199.     #endregion
  200.  
  201.     #region Properties
  202.  
  203.     [Category("Switched")]
  204.     public bool Switched
  205.     {
  206.         get { return _Switched; }
  207.  
  208.         set
  209.         {
  210.             _Switched = value;
  211.             Invalidate();
  212.         }
  213.     }
  214.  
  215.     [Category(" UnChecked "),
  216.     Description("Gets or sets the switch control color while unchecked")]
  217.     public Color UnCheckedColor
  218.     {
  219.         get
  220.         {
  221.             return _UnCheckedColor;
  222.         }
  223.         set
  224.         {
  225.             _UnCheckedColor = value;
  226.             Invalidate();
  227.         }
  228.     }
  229.  
  230.     [Category(" Checked "),
  231.     Description("Gets or sets the switch control color while checked")]
  232.     public Color CheckedColor
  233.     {
  234.         get
  235.         {
  236.             return _CheckedColor;
  237.         }
  238.         set
  239.         {
  240.             _CheckedColor = value;
  241.             Invalidate();
  242.         }
  243.     }
  244.  
  245.     [Category(" Checked "),
  246.     Description("Gets or sets the switch control ball color while checked")]
  247.     public Color CheckedBallColor
  248.     {
  249.         get
  250.         {
  251.             return _CheckedBallColor;
  252.         }
  253.         set
  254.         {
  255.             _CheckedBallColor = value;
  256.             Invalidate();
  257.         }
  258.     }
  259.  
  260.     [Category(" UnChecked "),
  261.     Description("Gets or sets the switch control ball color while unchecked")]
  262.     public Color UnCheckedBallColor
  263.     {
  264.         get
  265.         {
  266.             return _UnCheckedBallColor;
  267.         }
  268.         set
  269.         {
  270.             _UnCheckedBallColor = value;
  271.             Invalidate();
  272.         }
  273.     }
  274.  
  275.     #endregion
  276.  
  277.     #region Draw Control
  278.  
  279.     protected override void OnPaint(PaintEventArgs e)
  280.     {
  281.         Graphics G = e.Graphics;
  282.  
  283.         G.SmoothingMode = SmoothingMode.AntiAlias;
  284.         if (Switched)
  285.         {
  286.             H.FillRoundedPath(e.Graphics, new SolidBrush(CheckedColor), new Rectangle(0, 1, 28, 16), 16);
  287.             G.FillEllipse(new SolidBrush(CheckedBallColor), new Rectangle(Width - 17, 0, 16, 18));
  288.         }
  289.         else
  290.         {
  291.             H.FillRoundedPath(e.Graphics, new SolidBrush(UnCheckedColor), new Rectangle(0, 1, 28, 16), 16);
  292.             G.FillEllipse(new SolidBrush(UnCheckedBallColor), new Rectangle(Convert.ToInt32(0.5), 0, 16, 18));
  293.         }
  294.  
  295.  
  296.     }
  297.  
  298.     #endregion
  299.  
  300.     #region Events
  301.  
  302.     public event CheckedChangedEventHandler Switch;
  303.     public delegate void CheckedChangedEventHandler(object sender);
  304.  
  305.     protected override void OnClick(EventArgs e)
  306.     {
  307.         _Switched = !_Switched;
  308.         if (Switch != null)
  309.         {
  310.             Switch(this);
  311.         }
  312.         base.OnClick(e);
  313.         Invalidate();
  314.     }
  315.  
  316.     protected override void OnResize(EventArgs e)
  317.     {
  318.         base.OnResize(e);
  319.         Size = new Size(30, 19);
  320.         Invalidate();
  321.     }
  322.  
  323.     #endregion
  324.  
  325. }
  326. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement