1

I have a code that helped me make a rounded corner, border-less WinForm. It works fine but the thing is that it has no borders, so I want to add rounded borders to it. Also, I only want TopLeft and BottomRight corners to be rounded.

This is my current code:

public partial class mainForm : 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, // height of ellipse
        int nHeightEllipse // width of ellipse
    );
}

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

It is easily achievable in WPF but how do I get that in WinForms?

What should I do?

  • The code seems to be same as in [this question](https://stackoverflow.com/q/5092216/1997232). You can paint borders manually with `ControlPaint`. Or do you want to be [able to resize](https://stackoverflow.com/q/2575216/1997232) borderless form? – Sinatr May 15 '19 at 08:43
  • Yes, it is the same. Also, I am not thinking of resizing as of now but yes, border is the only thing I want. Along with that, I want to be able to just have two rounded corners. – Omanshu Chaurasia May 15 '19 at 08:48
  • [Fancy Windows Forms - CodeProject](https://www.codeproject.com/articles/33716/fancy-windows-forms?fid=1536616&df=90&mpp=25&sort=Position&view=Normal&spc=Relaxed&fr=101). – Jimi May 15 '19 at 09:23
  • 1
    CreateRoundRectRgn() does not give you only two rounded corners. Use the .NET Region(GraphicsPath) constructor instead. And you can't really ignore the likelihood that the window will resize itself so this needs to be done at least in the Load event, possibly in the Resize event. – Hans Passant May 15 '19 at 10:05

1 Answers1

3

You can draw border manually in client area. It's very simple but you will have to take care to layout children controls with some margin.

And it is still a challenge though, because there is only Graphics.FillRegion and no way to draw outline or DrawRegion method.

We can create GraphicsPath and draw it with Graphics.DrawPath, but creating it is tricky, e.g. this implementation doesn't match to to created with CreateRoundRectRgn() method.

So there is a trick with 2 regions: bigger region with border color and smaller region inside with client color. This will leave little bit of outer region which will visually creates a border.

readonly Region _client;

public Form1()
{
    InitializeComponent();
    // calculate smaller inner region using same method
    _client = Region.FromHrgn(CreateRoundRectRgn(1, 1, Width - 1, Height - 1, 20, 20));
    Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    // FillRectangle is faster than FillRegion for drawing outer bigger region
    // and it's actually not needed, you can simply set form BackColor to wanted border color
    // e.Graphics.FillRectangle(Brushes.Red, ClientRectangle);
    e.Graphics.FillRegion(Brushes.White, _client);
}

Result:

Sinatr
  • 20,892
  • 15
  • 90
  • 319