Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Directives
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Text;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- #endregion
- #region Browse
- /* Copyright & Contact
- * --------------------------------------------------------------------------------<
- * Tool Name : Switch Button *
- * From Project : Creators Eye *
- * Project Lang : C# *
- * Creator : Yassine Abbani *
- * Facebook : https://www.facebook.com/YassineAbbani.user *
- * Pastebin : https://pastebin.com/u/Yassine_Abbani *
- * Youtube : https://www.youtube.com/channel/UCqvOCRs3HWbPH4yuZuTx8mw *
- * Version : 1.0 Beta *
- * Color : Multi Color *
- * Style : Windowws 10 *
- *>--------------------------------------------------------------------------------<
- */
- /* Features
- * ------------------------
- * 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.
- * Creators Eye Windowws 10 Switch can be added at design time using drag and drop and at runtime
- * Possible customizations:
- * Ability to change on and off colors*/
- /* history
- * ------------------------
- * 1.0 (24 Feb 2018):
- * Custom Properties
- * Set the color of Ce Windowws 10 switch when ON using OnColor property
- * Set the color of Ce Windowws 10 switch when on OFF status using OffColor property
- */
- #endregion
- #region Import
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- using System.Drawing;
- using System.ComponentModel;
- #endregion
- #region Helper Methods
- public class HelperMethods
- {
- public GraphicsPath GP = null;
- public enum MouseMode
- {
- NormalMode,
- Hovered,
- Pushed
- };
- public void DrawImageFromBase64(Graphics G, string Base64Image, Rectangle Rect)
- {
- Image IM = null;
- using (System.IO.MemoryStream ms = new System.IO.MemoryStream(Convert.FromBase64String(Base64Image)))
- {
- IM = System.Drawing.Image.FromStream(ms);
- }
- G.DrawImage(IM, Rect);
- }
- public GraphicsPath RoundRec(Rectangle r, int Curve, bool TopLeft = true, bool TopRight = true, bool BottomLeft = true, bool BottomRight = true)
- {
- GraphicsPath CreateRoundPath = new GraphicsPath(FillMode.Winding);
- if (TopLeft)
- {
- CreateRoundPath.AddArc(r.X, r.Y, Curve, Curve, 180f, 90f);
- }
- else
- {
- CreateRoundPath.AddLine(r.X, r.Y, r.X, r.Y);
- }
- if (TopRight)
- {
- CreateRoundPath.AddArc(r.Right - Curve, r.Y, Curve, Curve, 270f, 90f);
- }
- else
- {
- CreateRoundPath.AddLine(r.Right - r.Width, r.Y, r.Width, r.Y);
- }
- if (BottomRight)
- {
- CreateRoundPath.AddArc(r.Right - Curve, r.Bottom - Curve, Curve, Curve, 0f, 90f);
- }
- else
- {
- CreateRoundPath.AddLine(r.Right, r.Bottom, r.Right, r.Bottom);
- }
- if (BottomLeft)
- {
- CreateRoundPath.AddArc(r.X, r.Bottom - Curve, Curve, Curve, 90f, 90f);
- }
- else
- {
- CreateRoundPath.AddLine(r.X, r.Bottom, r.X, r.Bottom);
- }
- CreateRoundPath.CloseFigure();
- return CreateRoundPath;
- }
- public void FillRoundedPath(Graphics G, Color C, Rectangle Rect, int Curve, bool TopLeft = true, bool TopRight = true, bool BottomLeft = true, bool BottomRight = true)
- {
- G.FillPath(new SolidBrush(C), RoundRec(Rect, Curve, TopLeft, TopRight, BottomLeft, BottomRight));
- }
- public void FillRoundedPath(Graphics G, Brush B, Rectangle Rect, int Curve, bool TopLeft = true, bool TopRight = true, bool BottomLeft = true, bool BottomRight = true)
- {
- G.FillPath(B, RoundRec(Rect, Curve, TopLeft, TopRight, BottomLeft, BottomRight));
- }
- 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)
- {
- G.DrawPath(new Pen(C, Size), RoundRec(Rect, Curve, TopLeft, TopRight, BottomLeft, BottomRight));
- }
- 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)
- {
- G.DrawLine(new Pen(C, Size), P1_0, P1_1);
- G.DrawLine(new Pen(C, Size), P2_0, P2_1);
- G.DrawLine(new Pen(C, Size), P3_0, P3_1);
- }
- public Pen PenRGBColor(int R, int G, int B, Single size)
- { return new Pen(System.Drawing.Color.FromArgb(R, G, B), size); }
- public Pen PenHTMlColor(String C_WithoutHash, float Thick)
- { return new Pen(GetHTMLColor(C_WithoutHash), Thick); }
- public SolidBrush SolidBrushRGBColor(int R, int G, int B, int A = 0)
- { return new SolidBrush(System.Drawing.Color.FromArgb(A, R, G, B)); }
- public SolidBrush SolidBrushHTMlColor(String C_WithoutHash)
- { return new SolidBrush(GetHTMLColor(C_WithoutHash)); }
- public Color GetHTMLColor(String C_WithoutHash)
- { return ColorTranslator.FromHtml("#" + C_WithoutHash); }
- public String ColorToHTML(Color C)
- { return ColorTranslator.ToHtml(C); }
- public Color SetARGB(int A, int R, int G, int B)
- { return System.Drawing.Color.FromArgb(A, R, G, B); }
- public Color SetRGB(int R, int G, int B)
- { return System.Drawing.Color.FromArgb(R, G, B); }
- public void CentreString(Graphics G, String Text, Font font, Brush brush, Rectangle Rect)
- { G.DrawString(Text, font, brush, new Rectangle(Rect.X, Rect.Y, Rect.Width, Rect.Height), new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }); }
- public void LeftString(Graphics G, String Text, Font font, Brush brush, Rectangle Rect)
- {
- 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 });
- }
- public void RightString(Graphics G, string Text, Font font, Brush brush, Rectangle Rect)
- {
- 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 });
- }
- }
- #endregion
- #region Switch
- [DefaultEvent("Switch")]
- public class Ce_W10_SwitchButton : Control
- {
- #region Variables
- private bool _Switched = false;
- private static HelperMethods H = new HelperMethods();
- private Color _UnCheckedColor = Color.Black;
- private Color _CheckedColor = H.GetHTMLColor("3075bb");
- private Color _CheckedBallColor = Color.White;
- private Color _UnCheckedBallColor = Color.Black;
- #endregion
- #region Constructors
- public Ce_W10_SwitchButton()
- {
- SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
- ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
- DoubleBuffered = true;
- UpdateStyles();
- BackColor = Color.Transparent;
- Cursor = Cursors.Hand;
- Font = new Font("Ubuntu", 10, FontStyle.Regular);
- Size = new Size(70, 28);
- }
- #endregion
- #region Properties
- [Category("Appearance")]
- public bool Switched
- {
- get { return _Switched; }
- set
- {
- _Switched = value;
- Invalidate();
- }
- }
- [Category(" Custom Properties "),
- Description("Gets or sets the switch control color while unchecked")]
- public Color UnCheckedColor
- {
- get
- {
- return _UnCheckedColor;
- }
- set
- {
- _UnCheckedColor = value;
- Invalidate();
- }
- }
- [Category(" Custom Properties "),
- Description("Gets or sets the switch control color while checked")]
- public Color CheckedColor
- {
- get
- {
- return _CheckedColor;
- }
- set
- {
- _CheckedColor = value;
- Invalidate();
- }
- }
- [Category(" Custom Properties "),
- Description("Gets or sets the switch control ball color while checked")]
- public Color CheckedBallColor
- {
- get
- {
- return _CheckedBallColor;
- }
- set
- {
- _CheckedBallColor = value;
- Invalidate();
- }
- }
- [Category(" Custom Properties "),
- Description("Gets or sets the switch control ball color while unchecked")]
- public Color UnCheckedBallColor
- {
- get
- {
- return _UnCheckedBallColor;
- }
- set
- {
- _UnCheckedBallColor = value;
- Invalidate();
- }
- }
- #endregion
- #region Draw Control
- protected override void OnPaint(PaintEventArgs e)
- {
- Graphics G = e.Graphics;
- G.SmoothingMode = SmoothingMode.AntiAlias;
- if (Switched)
- {
- H.FillRoundedPath(e.Graphics, new SolidBrush(CheckedColor), new Rectangle(0, 0, 40, 16), 16);
- G.FillEllipse(new SolidBrush(CheckedBallColor), new Rectangle(Width - Convert.ToInt32(14.5), Convert.ToInt32(2.7), 10, 10));
- }
- else
- {
- H.DrawRoundedPath(e.Graphics, UnCheckedColor, Convert.ToInt32(1.8), new Rectangle(0, 0, 40, 16), 16);
- G.FillEllipse(new SolidBrush(UnCheckedBallColor), new Rectangle(Convert.ToInt32(2.7), Convert.ToInt32(2.7), 10, 10));
- }
- }
- #endregion
- #region Events
- public event CheckedChangedEventHandler Switch;
- public delegate void CheckedChangedEventHandler(object sender);
- protected override void OnClick(EventArgs e)
- {
- _Switched = !_Switched;
- if (Switch != null)
- {
- Switch(this);
- }
- base.OnClick(e);
- Invalidate();
- }
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- Size = new Size(42, 18);
- }
- #endregion
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement