public class RoundButton : Button
{
bool isMouseEnter = false;
GraphicsPath GetRoundPath(RectangleF Rect, int radius)
{
float r2 = radius / 2f;
GraphicsPath GraphPath = new GraphicsPath();
GraphPath.AddArc(Rect.X-1, 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, 271, 90);
GraphPath.AddLine(Rect.Width, Rect.Y + r2, Rect.Width, Rect.Height - r2);
GraphPath.AddArc(Rect.X + Rect.Width - radius - 1,
Rect.Y + Rect.Height - radius, radius, radius, 1, 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);
GraphicsPath GraphPath = GetRoundPath(Rect, 10);
this.Region = new Region(GraphPath);
using (Pen pen = new Pen(System.Drawing.Color.FromArgb(74, 174, 74), 8.25F))
{
if (isMouseEnter)
pen.Color = System.Drawing.Color.FromArgb(88, 208, 88);
pen.Alignment = PenAlignment.Inset;
e.Graphics.DrawPath(pen, GraphPath);
}
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
isMouseEnter = true;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
isMouseEnter = false;
}
}