Healthcare IT Blog

A place to blog about Healthcare Informatics Software Development Implementations

image

How to round the edges of a control in C#

By Technical Team Technosoft |27 February 2015

The following code snippet will make the edges of a windows form round shaped.

The other windows controls including buttons, comboboxes, listboxes etc. could also be made round shaped. Just replace "this" with the control name when calling the function "GetRoundedRegion",

[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn

(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);
//function call, it will return the rounded region of the given region
public static System.Drawing.Region GetRoundedRegion(int controlWidth, int controlHeight)
{
return System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, controlWidth - 5, controlHeight - 5, 12, 12));
}

//the following Line of Code will round the edges of C# form
this.Region = GetRoundedRegion(this.Width, this.Height);