ich war auf der Suche nach einem 'chicen' Switch Button oder Toggle Button. Ich bin auf eine Lösung gestoßen,
die, wie ich finde, auch auf Deutsch verfügbar sein soll.
Die Herangesehensweise ist recht simple. Eine Klassen-Objekt erstellen und folgenden Code einfügen. Nach dem Compilieren steht der Button zur Verfügung.
All credits to: www.rjcodeadvance.com
Weitere Anpassungsmöglichkeiten:
codemengenbedingt TabMenü für beide Sprachen eingebaut ~VaporiZed
die, wie ich finde, auch auf Deutsch verfügbar sein soll.
Die Herangesehensweise ist recht simple. Eine Klassen-Objekt erstellen und folgenden Code einfügen. Nach dem Compilieren steht der Button zur Verfügung.
C#-Quellcode
- using System.Windows.Forms;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.ComponentModel;
- namespace MusterProject.Controls.BenutzerTools {
- /// <summary>
- /// 'Source: https://github.com/RJCodeAdvance/Toggle-Button-WinForm/blob/main/RJToggleButton.cs
- /// </summary>
- namespace CustomControls.ToggleButton {
- public class SwitchButton : CheckBox {
- //Fields
- private Color onBackColor = Color.MediumSlateBlue;
- private Color onToggleColor = Color.WhiteSmoke;
- private Color offBackColor = Color.Gray;
- private Color offToggleColor = Color.Gainsboro;
- private bool solidStyle = true;
- //Properties
- #region Properties [Category("OwnTools")]
- public Color OnBackColor {
- get {
- return onBackColor;
- }
- set {
- onBackColor = value;
- this.Invalidate();
- }
- }
- [Category("OwnTools")]
- public Color OnToggleColor {
- get {
- return onToggleColor;
- }
- set {
- onToggleColor = value;
- this.Invalidate();
- }
- }
- [Category("OwnTools")]
- public Color OffBackColor {
- get {
- return offBackColor;
- }
- set {
- offBackColor = value;
- this.Invalidate();
- }
- }
- [Category("OwnTools")]
- public Color OffToggleColor {
- get {
- return offToggleColor;
- }
- set {
- offToggleColor = value;
- this.Invalidate();
- }
- }
- [Browsable(false)]
- public override string Text {
- get {
- return base.Text;
- }
- set {
- }
- }
- [Category("OwnTools")]
- [DefaultValue(true)]
- public bool SolidStyle {
- get {
- return solidStyle;
- }
- set {
- solidStyle = value;
- this.Invalidate();
- }
- }
- #endregion
- /// Constructor
- public SwitchButton() {
- this.MinimumSize = new Size(45, 22);
- }
- //Methods
- private GraphicsPath GetFigurePath() {
- int arcSize = this.Height - 1;
- Rectangle leftArc = new Rectangle(0, 0, arcSize, arcSize);
- Rectangle rightArc = new Rectangle(this.Width - arcSize - 2, 0, arcSize, arcSize);
- GraphicsPath path = new GraphicsPath();
- path.StartFigure();
- path.AddArc(leftArc, 90, 180);
- path.AddArc(rightArc, 270, 180);
- path.CloseFigure();
- return path;
- }
- protected override void OnPaint(PaintEventArgs pevent) {
- int toggleSize = this.Height - 5;
- pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
- pevent.Graphics.Clear(this.Parent.BackColor);
- if (this.Checked) //ON
- {
- //Draw the control surface
- if (solidStyle)
- pevent.Graphics.FillPath(new SolidBrush(onBackColor), GetFigurePath());
- else pevent.Graphics.DrawPath(new Pen(onBackColor, 2), GetFigurePath());
- //Draw the toggle
- pevent.Graphics.FillEllipse(new SolidBrush(onToggleColor),
- new Rectangle(this.Width - this.Height + 1, 2, toggleSize, toggleSize));
- } else //OFF
- {
- //Draw the control surface
- if (solidStyle)
- pevent.Graphics.FillPath(new SolidBrush(offBackColor), GetFigurePath());
- else pevent.Graphics.DrawPath(new Pen(offBackColor, 2), GetFigurePath());
- //Draw the toggle
- pevent.Graphics.FillEllipse(new SolidBrush(offToggleColor),
- new Rectangle(2, 2, toggleSize, toggleSize));
- }
- }
- }
- }
- }
VB.NET-Quellcode
- Imports System.Windows.Forms
- Imports System.Drawing
- Imports System.Drawing.Drawing2D
- Imports System.ComponentModel
- Public Class SwitchButton
- Inherits CheckBox
- Private onBackColor As Color = Color.MediumSlateBlue
- Private onToggleColor As Color = Color.WhiteSmoke
- Private offBackColor As Color = Color.Gray
- Private offToggleColor As Color = Color.Gainsboro
- Private solidStyle As Boolean = True
- Public Property OnBackColor As Color
- Get
- Return onBackColor
- End Get
- Set(ByVal value As Color)
- onBackColor = value
- Me.Invalidate()
- End Set
- End Property
- <Category("OwnTools")>
- Public Property OnToggleColor As Color
- Get
- Return onToggleColor
- End Get
- Set(ByVal value As Color)
- onToggleColor = value
- Me.Invalidate()
- End Set
- End Property
- <Category("OwnTools")>
- Public Property OffBackColor As Color
- Get
- Return offBackColor
- End Get
- Set(ByVal value As Color)
- offBackColor = value
- Me.Invalidate()
- End Set
- End Property
- <Category("OwnTools")>
- Public Property OffToggleColor As Color
- Get
- Return offToggleColor
- End Get
- Set(ByVal value As Color)
- offToggleColor = value
- Me.Invalidate()
- End Set
- End Property
- <Browsable(False)>
- Public Overrides Property Text As String
- Get
- Return MyBase.Text
- End Get
- Set(ByVal value As String)
- End Set
- End Property
- <Category("OwnTools")>
- <DefaultValue(True)>
- Public Property SolidStyle As Boolean
- Get
- Return solidStyle
- End Get
- Set(ByVal value As Boolean)
- solidStyle = value
- Me.Invalidate()
- End Set
- End Property
- Public Sub New()
- Me.MinimumSize = New Size(45, 22)
- End Sub
- Private Function GetFigurePath() As GraphicsPath
- Dim arcSize As Integer = Me.Height - 1
- Dim leftArc As Rectangle = New Rectangle(0, 0, arcSize, arcSize)
- Dim rightArc As Rectangle = New Rectangle(Me.Width - arcSize - 2, 0, arcSize, arcSize)
- Dim path As GraphicsPath = New GraphicsPath()
- path.StartFigure()
- path.AddArc(leftArc, 90, 180)
- path.AddArc(rightArc, 270, 180)
- path.CloseFigure()
- Return path
- End Function
- Protected Overrides Sub OnPaint(ByVal pevent As PaintEventArgs)
- Dim toggleSize As Integer = Me.Height - 5
- pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias
- pevent.Graphics.Clear(Me.Parent.BackColor)
- If Me.Checked Then
- If solidStyle Then
- pevent.Graphics.FillPath(New SolidBrush(onBackColor), GetFigurePath())
- Else
- pevent.Graphics.DrawPath(New Pen(onBackColor, 2), GetFigurePath())
- End If
- pevent.Graphics.FillEllipse(New SolidBrush(onToggleColor), New Rectangle(Me.Width - Me.Height + 1, 2, toggleSize, toggleSize))
- Else
- If solidStyle Then
- pevent.Graphics.FillPath(New SolidBrush(offBackColor), GetFigurePath())
- Else
- pevent.Graphics.DrawPath(New Pen(offBackColor, 2), GetFigurePath())
- End If
- pevent.Graphics.FillEllipse(New SolidBrush(offToggleColor), New Rectangle(2, 2, toggleSize, toggleSize))
- End If
- End Sub
- End Class
All credits to: www.rjcodeadvance.com
Weitere Anpassungsmöglichkeiten:
codemengenbedingt TabMenü für beide Sprachen eingebaut ~VaporiZed
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „VaporiZed“ ()