22

I have a window without borders. I searched net for rounded corners but all with borders. How can i make rounded corners of the form(not with borders) ? Is there a way to do that?

I am a newbie to c#, so please explain...

Thanks

Hari krishnan
  • 2,060
  • 4
  • 18
  • 27

3 Answers3

65

try this:

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));
        }
    }
}

from here: Form with Rounded Borders in C#?

mikiqex
  • 5,023
  • 2
  • 24
  • 22
AsfK
  • 3,328
  • 4
  • 36
  • 73
  • Note that Width and Height are references to this.Width and this.Height. – Jason Harrison May 22 '14 at 12:39
  • 2
    Tried using this on a Panel. It only rounds one of the edges (top left)! – El Mac Jun 10 '14 at 12:04
  • 1
    @ElMac: I not try it again and it works for me great. could you verify you copy-paste the code correct please? – AsfK Jun 10 '14 at 14:08
  • 1
    I found the problem. If I make the Panel too small, some edges get cut off. I don't know the reason yet, but I can't use this approach because the panels I use should be almost button-sized. – El Mac Jun 11 '14 at 07:43
  • 1
    You may also need to add "this.AllowTransparency = true;" – Jason Harrison Nov 30 '14 at 05:51
  • 2
    the reason for cut corners is that the region is not resized with the window, you need to recreate it and reasign it on resize –  Sep 25 '15 at 12:20
2

The Region propery simply cuts off the corners. To have a true rounded corner you will have to draw the rounded rectangles.

Drawing rounded rectangles

It might be easier to draw an image of the shape you want and put that on the transparent form. Easier to draw but cannot be resized.

Also check this Another One

Community
  • 1
  • 1
  • "The Region propery [SIC] simply cuts off the corners." This is true, it clips the rendering of the form making the corners of the form have rounded transparent corners. In order to *draw* rounded corners, say with a 3D bevel appearance, additional drawing work is required. So you're not wrong, but I thought your phrasing was a just a bit confusing. – Jason Harrison May 22 '14 at 12:38
1

I found this code

To come up with the rounded corners textbox, I started trying to work with the paint override event, but unfortunately without any result, which is due to the fact (I assume) that the textbox is derived from Windows. Therefore, I tried overriding the WM_PAINT API instead, which had the desired results

http://www.codeproject.com/Articles/17453/Textbox-with-rounded-corners

Thanks

Moayad Myro
  • 294
  • 1
  • 3
  • 12