/// <summary> /// Initializes a new instance of the <see cref="ZeroitMetroSwitch" /> class. /// </summary> public ZeroitMetroSwitch() { this._Style = Design.Style.Light; this._Checked = false; this._DefaultColor = Design.MetroColors.LightSwitchRail; this._BorderColor = Design.MetroColors.LightBorder; this._CheckColor = Design.MetroColors.AccentBlue; this._SwitchColor = Design.MetroColors.LightDefault; this._HoverColor = Design.MetroColors.AccentLightBlue; this._SwitchStyle = ZeroitMetroSwitch.MetroSwitchStyle.Round; this._SwitchWidth = 15; this._RailWidth = 10; this._OnOffText = "OFF|ON"; this._OnText = "ON"; this._OffText = "OFF"; this._AutoStyle = true; this._MouseState = Helpers.MouseState.None; this.Font = new System.Drawing.Font("Segoe UI", 9f); this.SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true); this.UpdateStyles(); this.BackColor = Color.Transparent; }
/// <summary> /// Draws the switch. /// </summary> /// <param name="g">The g.</param> /// <param name="switchstyle">The switchstyle.</param> protected void DrawSwitch(Graphics g, ZeroitMetroSwitch.MetroSwitchStyle switchstyle) { Rectangle rectangle; Rectangle rectangle1; Rectangle rectangle2; Rectangle rectangle3; Rectangle rectangle4; switch (switchstyle) { case ZeroitMetroSwitch.MetroSwitchStyle.Round: { using (SolidBrush solidBrush = new SolidBrush((this._Checked ? this._DefaultColor : this._CheckColor))) { rectangle1 = new Rectangle(0, 1, checked (this.Width - 2), 16); Design.Drawing.FillRoundedPath(g, solidBrush, rectangle1, 16, true, true, true, true); if (this._Checked) { rectangle1 = new Rectangle(checked (this.Width - 19), 0, 18, 18); rectangle2 = rectangle1; } else { rectangle = new Rectangle(0, 0, 18, 18); rectangle2 = rectangle; } Rectangle rectangle5 = rectangle2; solidBrush.Color = this._SwitchColor; g.FillEllipse(solidBrush, rectangle5); using (Pen pen = new Pen(this._BorderColor)) { if ((this._MouseState == Helpers.MouseState.Over || this._MouseState == Helpers.MouseState.Pressed ? true : false)) { pen.Color = this._HoverColor; } g.DrawEllipse(pen, rectangle5); } } break; } case ZeroitMetroSwitch.MetroSwitchStyle.Rectangular: { using (SolidBrush solidBrush1 = new SolidBrush(this._DefaultColor)) { if (!this._Checked) { rectangle = new Rectangle(this._SwitchWidth, checked (checked (checked ((int)Math.Round((double)this.Height / 2)) - checked ((int)Math.Round((double)this._RailWidth / 2))) - 1), checked (this.Width - this._SwitchWidth), checked (this._RailWidth + 1)); rectangle3 = rectangle; } else { rectangle1 = new Rectangle(0, checked (checked (checked ((int)Math.Round((double)this.Height / 2)) - checked ((int)Math.Round((double)this._RailWidth / 2))) - 1), checked (this.Width - this._SwitchWidth), checked (this._RailWidth + 1)); rectangle3 = rectangle1; } Rectangle rectangle6 = rectangle3; solidBrush1.Color = (this._Checked ? this._DefaultColor : this._CheckColor); g.FillRectangle(solidBrush1, rectangle6); if (!this._Checked) { rectangle1 = new Rectangle(0, 0, this._SwitchWidth, checked (this.Height - 1)); rectangle4 = rectangle1; } else { rectangle = new Rectangle(checked (this.Width - this._SwitchWidth), 0, this._SwitchWidth, this.Height); rectangle4 = rectangle; } rectangle6 = rectangle4; solidBrush1.Color = this._SwitchColor; g.FillRectangle(solidBrush1, rectangle6); using (Pen pen1 = new Pen(this._BorderColor)) { if ((this._MouseState == Helpers.MouseState.Over || this._MouseState == Helpers.MouseState.Pressed ? true : false)) { pen1.Color = this._HoverColor; } rectangle1 = new Rectangle(rectangle6.X, rectangle6.Y, checked (rectangle6.Width - 1), checked (rectangle6.Height - 1)); g.DrawRectangle(pen1, rectangle1); } } break; } case ZeroitMetroSwitch.MetroSwitchStyle.OnOff: { Color color = Design.MetroColors.ChangeColorBrightness(this._DefaultColor, -0.4f); using (SolidBrush solidBrush2 = new SolidBrush((this._Checked ? color : this._DefaultColor))) { using (Pen pen2 = new Pen(this._BorderColor)) { rectangle1 = new Rectangle(checked ((int)Math.Round((double)this.Width / 2)), 0, checked ((int)Math.Round((double)this.Width / 2)), checked (this.Height - 1)); g.FillRectangle(solidBrush2, rectangle1); solidBrush2.Color = (this._Checked ? this._DefaultColor : color); rectangle1 = new Rectangle(0, 0, checked ((int)Math.Round((double)this.Width / 2)), checked (this.Height - 1)); g.FillRectangle(solidBrush2, rectangle1); rectangle1 = new Rectangle(checked ((int)Math.Round((double)this.Width / 2)), 0, checked ((int)Math.Round((double)this.Width / 2)), checked (this.Height - 1)); g.DrawRectangle(pen2, rectangle1); rectangle1 = new Rectangle(0, 0, checked (this.Width - 1), checked (this.Height - 1)); g.DrawRectangle(pen2, rectangle1); } color = Design.MetroColors.ChangeColorBrightness(this.ForeColor, -0.4f); solidBrush2.Color = (this._Checked ? color : this._CheckColor); if ((this._MouseState == Helpers.MouseState.Over || this._MouseState == Helpers.MouseState.Pressed ? true : false)) { solidBrush2.Color = (this._Checked ? solidBrush2.Color : this._HoverColor); } StringFormat stringFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; using (StringFormat stringFormat1 = stringFormat) { string str = this._OnText; System.Drawing.Font font = this.Font; rectangle1 = new Rectangle(checked ((int)Math.Round((double)this.Width / 2)), 0, checked ((int)Math.Round((double)this.Width / 2)), this.Height); g.DrawString(str, font, solidBrush2, rectangle1, stringFormat1); solidBrush2.Color = (this._Checked ? this._CheckColor : color); if ((this._MouseState == Helpers.MouseState.Over || this._MouseState == Helpers.MouseState.Pressed ? true : false)) { solidBrush2.Color = (this._Checked ? this._HoverColor : solidBrush2.Color); } string str1 = this._OffText; System.Drawing.Font font1 = this.Font; rectangle1 = new Rectangle(0, 0, checked ((int)Math.Round((double)this.Width / 2)), this.Height); g.DrawString(str1, font1, solidBrush2, rectangle1, stringFormat1); } } break; } } }