Advertisement
Yassine_Abbani

Windows 10 Button [Costume Control]

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