Advertisement
akaMeltDown

Toggle Button

Aug 30th, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.60 KB | None | 0 0
  1.  
  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.  
  13. namespace Eternal_Romance
  14. {
  15.     #region Toggle Button
  16.  
  17.     [DefaultEvent("ToggledChanged")]
  18.     class ER_Toggle : Control
  19.     {
  20.  
  21.         #region Designer
  22.  
  23.         //|------DO-NOT-REMOVE------|
  24.         //|---------CREDITS---------|
  25.  
  26.         // Pill class and functions were originally created by Tedd
  27.         // Last edited by Tedd on: 12/20/2013
  28.         // Modified by HazelDev on: 1/4/2014
  29.         // Modified by HGJosh on: 8/30/2018
  30.  
  31.         //|---------CREDITS---------|
  32.         //|------DO-NOT-REMOVE------|
  33.  
  34.         public class PillStyle
  35.         {
  36.             public bool Left;
  37.             public bool Right;
  38.         }
  39.  
  40.         public GraphicsPath Pill(Rectangle Rectangle, PillStyle PillStyle)
  41.         {
  42.             GraphicsPath functionReturnValue = default(GraphicsPath);
  43.             functionReturnValue = new GraphicsPath();
  44.  
  45.             if (PillStyle.Left)
  46.             {
  47.                 functionReturnValue.AddArc(new Rectangle(Rectangle.X, Rectangle.Y, Rectangle.Height, Rectangle.Height), -270, 180);
  48.             }
  49.             else
  50.             {
  51.                 functionReturnValue.AddLine(Rectangle.X, Rectangle.Y + Rectangle.Height, Rectangle.X, Rectangle.Y);
  52.             }
  53.  
  54.             if (PillStyle.Right)
  55.             {
  56.                 functionReturnValue.AddArc(new Rectangle(Rectangle.X + Rectangle.Width - Rectangle.Height, Rectangle.Y, Rectangle.Height, Rectangle.Height), -90, 180);
  57.             }
  58.             else
  59.             {
  60.                 functionReturnValue.AddLine(Rectangle.X + Rectangle.Width, Rectangle.Y, Rectangle.X + Rectangle.Width, Rectangle.Y + Rectangle.Height);
  61.             }
  62.  
  63.             functionReturnValue.CloseAllFigures();
  64.             return functionReturnValue;
  65.         }
  66.  
  67.         public object Pill(int X, int Y, int Width, int Height, PillStyle PillStyle)
  68.         {
  69.             return Pill(new Rectangle(X, Y, Width, Height), PillStyle);
  70.         }
  71.  
  72.         #endregion
  73.         #region Enums
  74.  
  75.         public enum _Type
  76.         {
  77.             YesNo,
  78.             OnOff,
  79.             IO
  80.         }
  81.  
  82.         #endregion
  83.         #region Variables
  84.  
  85.         private Timer AnimationTimer = new Timer { Interval = 1 };
  86.         private int ToggleLocation = 0;
  87.         public event ToggledChangedEventHandler ToggledChanged;
  88.         public delegate void ToggledChangedEventHandler();
  89.         private bool _Toggled;
  90.         private _Type ToggleType;
  91.         private Rectangle Bar;
  92.         private Size cHandle = new Size(15, 20);
  93.  
  94.         #endregion
  95.         #region Properties
  96.  
  97.         public bool Toggled
  98.         {
  99.             get { return _Toggled; }
  100.             set
  101.             {
  102.                 _Toggled = value;
  103.                 Invalidate();
  104.  
  105.                 if (ToggledChanged != null)
  106.                 {
  107.                     ToggledChanged();
  108.                 }
  109.             }
  110.         }
  111.  
  112.         public _Type Type
  113.         {
  114.             get { return ToggleType; }
  115.             set
  116.             {
  117.                 ToggleType = value;
  118.                 Invalidate();
  119.             }
  120.         }
  121.  
  122.         #endregion
  123.         #region EventArgs
  124.  
  125.         protected override void OnResize(EventArgs e)
  126.         {
  127.             base.OnResize(e);
  128.             Width = 41;
  129.             Height = 23;
  130.         }
  131.  
  132.         protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
  133.         {
  134.             base.OnMouseUp(e);
  135.             Toggled = !Toggled;
  136.         }
  137.  
  138.         #endregion
  139.  
  140.         public ER_Toggle()
  141.         {
  142.             SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
  143.             AnimationTimer.Tick += new EventHandler(AnimationTimer_Tick);
  144.         }
  145.         protected override void OnHandleCreated(EventArgs e)
  146.         {
  147.             base.OnHandleCreated(e);
  148.             AnimationTimer.Start();
  149.         }
  150.  
  151.         void AnimationTimer_Tick(object sender, EventArgs e)
  152.         {
  153.             //  Create a slide animation when toggled on/off
  154.             if ((_Toggled == true))
  155.             {
  156.                 if ((ToggleLocation < 100))
  157.                 {
  158.                     ToggleLocation += 10;
  159.                     this.Invalidate(false);
  160.                 }
  161.             }
  162.             else if ((ToggleLocation > 0))
  163.             {
  164.                 ToggleLocation -= 10;
  165.                 this.Invalidate(false);
  166.             }
  167.         }
  168.  
  169.         protected override void OnPaint(PaintEventArgs e)
  170.         {
  171.             base.OnPaint(e);
  172.             Graphics G = e.Graphics;
  173.             G.Clear(Parent.BackColor);
  174.             checked
  175.             {
  176.                 Point point = new Point(0, (int)Math.Round(unchecked((double)this.Height / 2.0 - (double)this.cHandle.Height / 2.0)));
  177.                 Point arg_A8_0 = point;
  178.                 Point point2 = new Point(0, (int)Math.Round(unchecked((double)this.Height / 2.0 + (double)this.cHandle.Height / 2.0)));
  179.                 LinearGradientBrush Gradient = new LinearGradientBrush(arg_A8_0, point2, Color.FromArgb(250, 250, 250), Color.FromArgb(240, 240, 240));
  180.                 this.Bar = new Rectangle(8, 10, this.Width - 21, this.Height - 21);
  181.  
  182.                 G.SmoothingMode = SmoothingMode.AntiAlias;
  183.                 G.FillPath(Gradient, (GraphicsPath)this.Pill(0, (int)Math.Round(unchecked((double)this.Height / 2.0 - (double)this.cHandle.Height / 2.0)), this.Width - 1, this.cHandle.Height - 5, new ER_Toggle.PillStyle
  184.                 {
  185.                     Left = true,
  186.                     Right = true
  187.                 }));
  188.                 G.DrawPath(new Pen(Color.FromArgb(177, 177, 176)), (GraphicsPath)this.Pill(0, (int)Math.Round(unchecked((double)this.Height / 2.0 - (double)this.cHandle.Height / 2.0)), this.Width - 1, this.cHandle.Height - 5, new ER_Toggle.PillStyle
  189.                 {
  190.                     Left = true,
  191.                     Right = true
  192.                 }));
  193.                 Gradient.Dispose();
  194.                 switch (this.ToggleType)
  195.                 {
  196.                     case ER_Toggle._Type.YesNo:
  197.                         {
  198.                             bool toggled = this.Toggled;
  199.                             if (toggled)
  200.                             {
  201.                                 G.DrawString("On", new Font("Segoe UI", 7f, FontStyle.Regular), Brushes.DimGray, (float)(this.Bar.X + 7), (float)this.Bar.Y, new StringFormat
  202.                                 {
  203.                                     Alignment = StringAlignment.Center,
  204.                                     LineAlignment = StringAlignment.Center
  205.                                 });
  206.                             }
  207.                             else
  208.                             {
  209.                                 G.DrawString("Off", new Font("Segoe UI", 7f, FontStyle.Regular), Brushes.DimGray, (float)(this.Bar.X + 18), (float)this.Bar.Y, new StringFormat
  210.                                 {
  211.                                     Alignment = StringAlignment.Center,
  212.                                     LineAlignment = StringAlignment.Center
  213.                                 });
  214.                             }
  215.                             break;
  216.                         }
  217.                     case ER_Toggle._Type.OnOff:
  218.                         {
  219.                             bool toggled = this.Toggled;
  220.                             if (toggled)
  221.                             {
  222.                                 G.DrawString("On", new Font("Segoe UI", 7f, FontStyle.Regular), Brushes.DimGray, (float)(this.Bar.X + 7), (float)this.Bar.Y, new StringFormat
  223.                                 {
  224.                                     Alignment = StringAlignment.Center,
  225.                                     LineAlignment = StringAlignment.Center
  226.                                 });
  227.                             }
  228.                             else
  229.                             {
  230.                                 G.DrawString("Off", new Font("Segoe UI", 7f, FontStyle.Regular), Brushes.DimGray, (float)(this.Bar.X + 18), (float)this.Bar.Y, new StringFormat
  231.                                 {
  232.                                     Alignment = StringAlignment.Center,
  233.                                     LineAlignment = StringAlignment.Center
  234.                                 });
  235.                             }
  236.                             break;
  237.                         }
  238.                     case ER_Toggle._Type.IO:
  239.                         {
  240.                             bool toggled = this.Toggled;
  241.                             if (toggled)
  242.                             {
  243.                                 G.DrawString("I", new Font("Segoe UI", 7f, FontStyle.Regular), Brushes.DimGray, (float)(this.Bar.X + 7), (float)this.Bar.Y, new StringFormat
  244.                                 {
  245.                                     Alignment = StringAlignment.Center,
  246.                                     LineAlignment = StringAlignment.Center
  247.                                 });
  248.                             }
  249.                             else
  250.                             {
  251.                                 G.DrawString("O", new Font("Segoe UI", 7f, FontStyle.Regular), Brushes.DimGray, (float)(this.Bar.X + 18), (float)this.Bar.Y, new StringFormat
  252.                                 {
  253.                                     Alignment = StringAlignment.Center,
  254.                                     LineAlignment = StringAlignment.Center
  255.                                 });
  256.                             }
  257.                             break;
  258.                         }
  259.                 }
  260.                 G.FillEllipse(new SolidBrush(Color.SlateBlue), this.Bar.X + (int)Math.Round(unchecked((double)this.Bar.Width * ((double)this.ToggleLocation / 80.0))) - (int)Math.Round((double)this.cHandle.Width / 2.0), this.Bar.Y + (int)Math.Round((double)this.Bar.Height / 2.0) - (int)Math.Round(unchecked((double)this.cHandle.Height / 2.0 - 1.0)), this.cHandle.Width, this.cHandle.Height - 5);
  261.                 G.DrawEllipse(new Pen(Color.FromArgb(177, 177, 176)), this.Bar.X + (int)Math.Round(unchecked((double)this.Bar.Width * ((double)this.ToggleLocation / 80.0) - (double)checked((int)Math.Round((double)this.cHandle.Width / 2.0)))), this.Bar.Y + (int)Math.Round((double)this.Bar.Height / 2.0) - (int)Math.Round(unchecked((double)this.cHandle.Height / 2.0 - 1.0)), this.cHandle.Width, this.cHandle.Height - 5);
  262.             }
  263.         }
  264.     }
  265.     #endregion
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement