Contact Us

Contact Us

How To Round The Edges Of A Control In C#

🚀 Quick Implementation

Use the GDI32 CreateRoundRectRgn function to modify the Control.Region property.

[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]

Best for: Windows Forms, Buttons, and Custom UI Panels.

To round the edges of a control in C#, you must use the GDI32.dll library to call the CreateRoundRectRgn function. This Win32 API method creates a rectangular region with rounded corners, which can then be applied to the Region The property of any Windows Form control (like buttons or panels) to change its physical shape at runtime.

How to Round The Edges Of A Control In C#

This image of a code snippet describes how to round the edges of a control in C#. The following code snippet will make the edges of windows form round-shaped. 

The other window controls, including buttons, combo boxes, list boxes, 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);

The Rounding Logic

The Rounding Logic-How To Round The Edges Of A Control In C#- Technosoft solutions

Healthcare software development, HL7 Interface, EMR/EHR integration, mHealth, HIPAA compliant

Frequently Asked Questions

Can I round corners in C# without using Win32 API?

In Windows Forms, using the Win32 API (CreateRoundRectRgn) is the most common method. However, in WPF (Windows Presentation Foundation), you can simply set the CornerRadius property on a Border control that contains your element, which is a much more modern and flexible approach.

Does rounding corners affect the control’s click area?

Yes. When you set the Region A property of a control, the operating system only recognizes mouse clicks and events within that specific region. Areas outside the rounded corners will become transparent and non-interactive.

Need help with a complex C# implementation? Get a Quote for Custom C# Development.

Talk to Technosoft .NET Experts