Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Text;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Windows.Forms;
- namespace Eternal_Romance
- {
- #region Toggle Button
- [DefaultEvent("ToggledChanged")]
- class ER_Toggle : Control
- {
- #region Designer
- //|------DO-NOT-REMOVE------|
- //|---------CREDITS---------|
- // Pill class and functions were originally created by Tedd
- // Last edited by Tedd on: 12/20/2013
- // Modified by HazelDev on: 1/4/2014
- // Modified by HGJosh on: 8/30/2018
- //|---------CREDITS---------|
- //|------DO-NOT-REMOVE------|
- public class PillStyle
- {
- public bool Left;
- public bool Right;
- }
- public GraphicsPath Pill(Rectangle Rectangle, PillStyle PillStyle)
- {
- GraphicsPath functionReturnValue = default(GraphicsPath);
- functionReturnValue = new GraphicsPath();
- if (PillStyle.Left)
- {
- functionReturnValue.AddArc(new Rectangle(Rectangle.X, Rectangle.Y, Rectangle.Height, Rectangle.Height), -270, 180);
- }
- else
- {
- functionReturnValue.AddLine(Rectangle.X, Rectangle.Y + Rectangle.Height, Rectangle.X, Rectangle.Y);
- }
- if (PillStyle.Right)
- {
- functionReturnValue.AddArc(new Rectangle(Rectangle.X + Rectangle.Width - Rectangle.Height, Rectangle.Y, Rectangle.Height, Rectangle.Height), -90, 180);
- }
- else
- {
- functionReturnValue.AddLine(Rectangle.X + Rectangle.Width, Rectangle.Y, Rectangle.X + Rectangle.Width, Rectangle.Y + Rectangle.Height);
- }
- functionReturnValue.CloseAllFigures();
- return functionReturnValue;
- }
- public object Pill(int X, int Y, int Width, int Height, PillStyle PillStyle)
- {
- return Pill(new Rectangle(X, Y, Width, Height), PillStyle);
- }
- #endregion
- #region Enums
- public enum _Type
- {
- YesNo,
- OnOff,
- IO
- }
- #endregion
- #region Variables
- private Timer AnimationTimer = new Timer { Interval = 1 };
- private int ToggleLocation = 0;
- public event ToggledChangedEventHandler ToggledChanged;
- public delegate void ToggledChangedEventHandler();
- private bool _Toggled;
- private _Type ToggleType;
- private Rectangle Bar;
- private Size cHandle = new Size(15, 20);
- #endregion
- #region Properties
- public bool Toggled
- {
- get { return _Toggled; }
- set
- {
- _Toggled = value;
- Invalidate();
- if (ToggledChanged != null)
- {
- ToggledChanged();
- }
- }
- }
- public _Type Type
- {
- get { return ToggleType; }
- set
- {
- ToggleType = value;
- Invalidate();
- }
- }
- #endregion
- #region EventArgs
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- Width = 41;
- Height = 23;
- }
- protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
- {
- base.OnMouseUp(e);
- Toggled = !Toggled;
- }
- #endregion
- public ER_Toggle()
- {
- SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
- AnimationTimer.Tick += new EventHandler(AnimationTimer_Tick);
- }
- protected override void OnHandleCreated(EventArgs e)
- {
- base.OnHandleCreated(e);
- AnimationTimer.Start();
- }
- void AnimationTimer_Tick(object sender, EventArgs e)
- {
- // Create a slide animation when toggled on/off
- if ((_Toggled == true))
- {
- if ((ToggleLocation < 100))
- {
- ToggleLocation += 10;
- this.Invalidate(false);
- }
- }
- else if ((ToggleLocation > 0))
- {
- ToggleLocation -= 10;
- this.Invalidate(false);
- }
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- Graphics G = e.Graphics;
- G.Clear(Parent.BackColor);
- checked
- {
- Point point = new Point(0, (int)Math.Round(unchecked((double)this.Height / 2.0 - (double)this.cHandle.Height / 2.0)));
- Point arg_A8_0 = point;
- Point point2 = new Point(0, (int)Math.Round(unchecked((double)this.Height / 2.0 + (double)this.cHandle.Height / 2.0)));
- LinearGradientBrush Gradient = new LinearGradientBrush(arg_A8_0, point2, Color.FromArgb(250, 250, 250), Color.FromArgb(240, 240, 240));
- this.Bar = new Rectangle(8, 10, this.Width - 21, this.Height - 21);
- G.SmoothingMode = SmoothingMode.AntiAlias;
- 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
- {
- Left = true,
- Right = true
- }));
- 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
- {
- Left = true,
- Right = true
- }));
- Gradient.Dispose();
- switch (this.ToggleType)
- {
- case ER_Toggle._Type.YesNo:
- {
- bool toggled = this.Toggled;
- if (toggled)
- {
- G.DrawString("On", new Font("Segoe UI", 7f, FontStyle.Regular), Brushes.DimGray, (float)(this.Bar.X + 7), (float)this.Bar.Y, new StringFormat
- {
- Alignment = StringAlignment.Center,
- LineAlignment = StringAlignment.Center
- });
- }
- else
- {
- G.DrawString("Off", new Font("Segoe UI", 7f, FontStyle.Regular), Brushes.DimGray, (float)(this.Bar.X + 18), (float)this.Bar.Y, new StringFormat
- {
- Alignment = StringAlignment.Center,
- LineAlignment = StringAlignment.Center
- });
- }
- break;
- }
- case ER_Toggle._Type.OnOff:
- {
- bool toggled = this.Toggled;
- if (toggled)
- {
- G.DrawString("On", new Font("Segoe UI", 7f, FontStyle.Regular), Brushes.DimGray, (float)(this.Bar.X + 7), (float)this.Bar.Y, new StringFormat
- {
- Alignment = StringAlignment.Center,
- LineAlignment = StringAlignment.Center
- });
- }
- else
- {
- G.DrawString("Off", new Font("Segoe UI", 7f, FontStyle.Regular), Brushes.DimGray, (float)(this.Bar.X + 18), (float)this.Bar.Y, new StringFormat
- {
- Alignment = StringAlignment.Center,
- LineAlignment = StringAlignment.Center
- });
- }
- break;
- }
- case ER_Toggle._Type.IO:
- {
- bool toggled = this.Toggled;
- if (toggled)
- {
- G.DrawString("I", new Font("Segoe UI", 7f, FontStyle.Regular), Brushes.DimGray, (float)(this.Bar.X + 7), (float)this.Bar.Y, new StringFormat
- {
- Alignment = StringAlignment.Center,
- LineAlignment = StringAlignment.Center
- });
- }
- else
- {
- G.DrawString("O", new Font("Segoe UI", 7f, FontStyle.Regular), Brushes.DimGray, (float)(this.Bar.X + 18), (float)this.Bar.Y, new StringFormat
- {
- Alignment = StringAlignment.Center,
- LineAlignment = StringAlignment.Center
- });
- }
- break;
- }
- }
- 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);
- 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);
- }
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement