C# Exercises

Home AgriMetSoft About Contact

Make Rounded and Flat Button in WinForms C#

	    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rounded_Flat_Button
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      button1.BackColor = Color.GreenYellow;
      button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
      button1.FlatAppearance.BorderColor = Color.Black;
      button1.FlatAppearance.BorderSize = 2;

      button2.BackColor = Color.OrangeRed;
      button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
      button2.FlatAppearance.BorderColor = Color.Black;
      button2.FlatAppearance.BorderSize = 1;

      button3.BackColor = Color.SlateBlue;
      button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
      button3.FlatAppearance.BorderColor = Color.Black;
      button3.FlatAppearance.BorderSize = 1;

      button1.Click += button1_Click;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      RoundedButton rb = new RoundedButton();
      rb.Text = "Rounded Button";
      rb.Height = 46;
      rb.Width = 139;
      rb.Location = new Point(120, 20);
      rb.rdus = 30;
      rb.BackColor = Color.IndianRed;
      rb.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
      rb.FlatAppearance.BorderColor = Color.Black;
      rb.FlatAppearance.BorderSize = 1;

      this.Controls.Add(rb);
    }
  }



  class RoundedButton : Button
  {
    public int rdus = 10;
    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


How to Make Rounded Button in C# | Flat Button in WinForms


List of Exercises