Sometimes you want to set rounded corners to your windows forms but by default windows forms haven't got support. You need to custom your windows forms by overriding the OnPaint method.
OK, Now we start creating a new form, then override the OnPaint method
protected override void OnPaint(PaintEventArgs e)
{
//Form with Rounded Borders in C#
Rectangle Bounds = new Rectangle(0, 0, this.Width, this.Height);
int CornerRadius = 20;
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90);
path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90);
path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
path.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
path.CloseAllFigures();
this.Region = new Region(path);
base.OnPaint(e);
}
Remember set the FormBorderStyle properties is None
Run your program, you can get the image as above
The second way you can use Win32 api to add rounded corners to your windows forms as below
public partial class frmBorderRound : Form
{
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
public frmBorderRound()
{
InitializeComponent();
this.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 10, 10));
}
}
Note you need to release IntPrt handle when you use CreateRoundRectRgn(), i think you should free it with DeleteObject() after your form closes. You can modify your code as below
public partial class frmBorderRound : Form
{
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
[DllImport("Gdi32.dll", EntryPoint = "DeleteObject")]
public static extern bool DeleteObject(IntPtr hObject);
IntPtr handle;
public frmBorderRound()
{
InitializeComponent();
handle = CreateRoundRectRgn(0, 0, Width, Height, 10, 10);
if (handle != IntPtr.Zero)
this.Region = System.Drawing.Region.FromHrgn(handle);
}
private void frmBorderRound_FormClosing(object sender, FormClosingEventArgs e)
{
if (handle != IntPtr.Zero)
DeleteObject(handle);//Release handle
}
}