How to make a button in WinForms with rounded corners? An example of a button is attached enter image description here

2 answers 2

Create a RoundButton class:

 public class RoundButton : Button { GraphicsPath GetRoundPath(RectangleF Rect, int radius) { float r2 = radius / 2f; GraphicsPath GraphPath = new 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); GraphicsPath GraphPath = GetRoundPath(Rect, 50); this.Region = new Region(GraphPath); using (Pen pen = new Pen(Color.Lime, 1.75f)) { pen.Alignment = PenAlignment.Inset; e.Graphics.DrawPath(pen, GraphPath); } } } 

Find the RoundButton in the ToolBox and drag it onto the form:

enter image description here

  • It is not efficient to calculate the GraphicsPath and set the Region for each drawing event. - Alexander Petrov
  • one
    What is a more effective option? - Alexander Puzanov
  • And why the edges are not smooth, as in the screenshot that laid out the vehicle? - Max
  • There are not only edges, but also a shadow on the button. But the question was how to round it off. - Alexander Puzanov
  • 2
    @AlexanderPuzanov, that’s what you need: http://www.cyberforum.ru/post9872749.html - Maxim

Thank you, Maxim. Your comment is the best answer.

 public class RoundButton : Control { public Color BackColor2 { get; set; } public Color ButtonBorderColor { get; set; } public int ButtonRoundRadius { get; set; } public Color ButtonHighlightColor { get; set; } public Color ButtonHighlightColor2 { get; set; } public Color ButtonHighlightForeColor { get; set; } public Color ButtonPressedColor { get; set; } public Color ButtonPressedColor2 { get; set; } public Color ButtonPressedForeColor { get; set; } private bool IsHighlighted; private bool IsPressed; public RoundButton() { Size = new Size(100, 40); ButtonRoundRadius = 30; BackColor = Color.Gainsboro; BackColor2 = Color.Silver; ButtonBorderColor = Color.Black; ButtonHighlightColor = Color.Orange; ButtonHighlightColor2 = Color.OrangeRed; ButtonHighlightForeColor = Color.Black; ButtonPressedColor = Color.Red; ButtonPressedColor2 = Color.Maroon; ButtonPressedForeColor = Color.White; } protected override CreateParams CreateParams { get { CreateParams createParams = base.CreateParams; createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT return createParams; } } protected override void OnPaint(PaintEventArgs e) { e.Graphics.SmoothingMode = SmoothingMode.HighQuality; var foreColor = IsPressed ? ButtonPressedForeColor : IsHighlighted ? ButtonHighlightForeColor : ForeColor; var backColor = IsPressed ? ButtonPressedColor : IsHighlighted ? ButtonHighlightColor : BackColor; var backColor2 = IsPressed ? ButtonPressedColor2 : IsHighlighted ? ButtonHighlightColor2 : BackColor2; using (var pen = new Pen(ButtonBorderColor, 1)) e.Graphics.DrawPath(pen, Path); using (var brush = new LinearGradientBrush(ClientRectangle, backColor, backColor2, LinearGradientMode.Vertical)) e.Graphics.FillPath(brush, Path); using (var brush = new SolidBrush(foreColor)) { var sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; var rect = ClientRectangle; rect.Inflate(-4, -4); e.Graphics.DrawString(Text, Font, brush, rect, sf); } base.OnPaint(e); } protected override void OnPaintBackground(PaintEventArgs e) { } protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); IsHighlighted = true; Parent.Invalidate(Bounds, false); Invalidate(); } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); IsHighlighted = false; IsPressed = false; Parent.Invalidate(Bounds, false); Invalidate(); } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); Parent.Invalidate(Bounds, false); Invalidate(); IsPressed = true; } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); Parent.Invalidate(Bounds, false); Invalidate(); IsPressed = false; } protected GraphicsPath Path { get { var rect = ClientRectangle; rect.Inflate(-1, -1); return GetRoundedRectangle(rect, ButtonRoundRadius); } } public static GraphicsPath GetRoundedRectangle(Rectangle rect, int d) { var gp = new GraphicsPath(); gp.AddArc(rect.X, rect.Y, d, d, 180, 90); gp.AddArc(rect.X + rect.Width - d, rect.Y, d, d, 270, 90); gp.AddArc(rect.X + rect.Width - d, rect.Y + rect.Height - d, d, d, 0, 90); gp.AddArc(rect.X, rect.Y + rect.Height - d, d, d, 90, 90); gp.CloseFigure(); return gp; } } 

A source

  • one
    Only this is not my option, but Storm23 from another forum. - Maxim