Rounded Corners in C# WinForms | Modern UI

x32x01
  • by x32x01 ||
Want to give your Windows Forms app a modern and stylish look? 👨‍💻 One simple tweak can make your form stand out - rounded corners!
In this guide, you’ll learn how to create smooth, rounded edges for any C# WinForms window using the GDI32 API.

🧠 Why Add Rounded Corners?​

Rounded corners make your app look cleaner and more polished - similar to modern Windows 11 apps. 🪟
Instead of using the default rectangular edges, you can easily customize your form’s region with just a few lines of code.

💻 Full C# Code Example​

Here’s the complete C# code to create a borderless form with rounded corners 👇
C#:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [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, // width of ellipse
            int nHeightEllipse // height of ellipse
        );

        public Form1()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
        }
    }
}



⚙️ Code Explanation​

Let’s break down what’s happening step by step:

1️⃣ Importing the Windows API​

The [DllImport] attribute lets you use functions from native DLLs.
Here, CreateRoundRectRgn is imported from Gdi32.dll, part of the Windows GDI (Graphics Device Interface).

2️⃣ Defining the Function​

This function creates a region in the shape of a rounded rectangle.
You provide:
  • Form dimensions (Width, Height)
  • Corner radius (20, 20 in this example)

3️⃣ Removing Default Borders​

this.FormBorderStyle = FormBorderStyle.None;
This removes the default rectangular window frame so your custom rounded region can take over.

4️⃣ Applying the Rounded Region​

Finally,
C#:
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(...));
assigns the new region to your form, making it display with smooth rounded edges 🎨



🧩 Customize the Corner Radius​

You can change the last two parameters in CreateRoundRectRgn to adjust the curve:
C#:
Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 40, 40));
Higher numbers → more rounded corners
Lower numbers → subtle curve



🪄 Pro Tip: Add a Drop Shadow​

If you’re removing borders, your window can look flat.
To fix that, add a drop shadow effect using the Windows API or form painting methods for a sleek floating look ✨

🚀 Wrap-Up​

That’s it! With a few lines of code, you can give your old-school WinForms UI a modern refresh. Rounded corners make your C# applications look more elegant, especially when combined with custom colors or gradient backgrounds 🎨
Now you’re one step closer to building professional-looking desktop apps with C# and .NET!
 
Last edited:
Related Threads
x32x01
Replies
0
Views
946
x32x01
x32x01
x32x01
Replies
0
Views
248
x32x01
x32x01
x32x01
Replies
0
Views
910
x32x01
x32x01
x32x01
Replies
0
Views
1K
x32x01
x32x01
Register & Login Faster
Forgot your password?
Forum Statistics
Threads
654
Messages
662
Members
66
Latest Member
medhatmalak
Back
Top