using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace Styling_Toggle_Button
{
public class SButton : 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
public Color OnBackColor
{
get { return onBackColor; }
set
{
onBackColor = value;
this.Invalidate();
}
}
public Color OnToggleColor
{
get { return onToggleColor; }
set
{
onToggleColor = value;
this.Invalidate();
}
}
public Color OffBackColor
{
get { return offBackColor; }
set
{
offBackColor = value;
this.Invalidate();
}
}
public Color OffToggleColor
{
get { return offToggleColor; }
set
{
offToggleColor = value;
this.Invalidate();
}
}
[Browsable(false)]
public override string Text
{
get { return base.Text; }
set { }
}
[DefaultValue(true)]
public bool SolidStyle
{
get { return solidStyle; }
set
{
solidStyle = value;
this.Invalidate();
}
}
//Constructor
public SButton()
{
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));
}
}
}
public class RoundedButton : Button
{
public RoundedButton()
{
this.BackColor = Color.OrangeRed;
this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.FlatAppearance.BorderColor = Color.Black;
this.FlatAppearance.BorderSize = 1;
this.Width = 140;
this.Height = 45;
}
public int rdus = 30;
System.Drawing.Drawing2D.GraphicsPath GetRoundPath(RectangleF Rect, int radius)
{
float r2 = radius / 2f;
System.Drawing.Drawing2D.GraphicsPath GraphPath = new System.Drawing.Drawing2D.GraphicsPath();
GraphPath.AddArc(Rect.X, Rect.Y, radius, radius, 180, 90);
GraphPath.AddLine(Rect.X + r2, Rect.Y, Rect.Width - r2, Rect.Y);
GraphPath.AddArc(Rect.X + Rect.Width - radius, Rect.Y, radius, radius, 270, 90);
GraphPath.AddLine(Rect.Width, Rect.Y + r2, Rect.Width, Rect.Height - r2);
GraphPath.AddArc(Rect.X + Rect.Width - radius,
Rect.Y + Rect.Height - radius, radius, radius, 0, 90);
GraphPath.AddLine(Rect.Width - r2, Rect.Height, Rect.X + r2, Rect.Height);
GraphPath.AddArc(Rect.X, Rect.Y + Rect.Height - radius, radius, radius, 90, 90);
GraphPath.AddLine(Rect.X, Rect.Height - r2, Rect.X, Rect.Y + r2);
GraphPath.CloseFigure();
return GraphPath;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
RectangleF Rect = new RectangleF(0, 0, this.Width, this.Height);
using (System.Drawing.Drawing2D.GraphicsPath GraphPath = GetRoundPath(Rect, rdus))
{
this.Region = new Region(GraphPath);
using (Pen pen = new Pen(Color.CadetBlue, 1.75f))
{
pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
e.Graphics.DrawPath(pen, GraphPath);
}
}
}
}
}
Download the project of Visual Studio 2013 in DropBox Download