Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a User Control with rounded corners?

I am trying to have a User Control that has rounded corners. It doesnt have a fixed size but it usually doesnt have a width much more than 120 pixels.

I need the User Control and its contents(a label and a table) to have rounded edges and look like a round box.

I have used this code.

[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 static System.Drawing.Region GetRoundedRegion(int controlWidth, int controlHeight)
    {
            return System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, controlWidth - 5, controlHeight - 5, 20, 20));
    } 

This gives the control rounded corners but after it has been running afew times and i have added multiples of my User Control to the form it will cause a leak and i will get the whitebox with a red cross on my user controls.

Is there a better way of doing this?

like image 960
CJM Avatar asked Oct 07 '15 08:10

CJM


People also ask

How can you created rounded corners using CSS?

To create a rounded corner, we use the CSS border-radius property. This property is used to set the border-radius of element.

How do you add rounded corners to an element?

The border-radius property is used to add rounded corners to an element in CSS. The border-radius property is shorthand for the four subproperties used to add rounded corners to each corner of a web element.

How do I make rounded corners in office?

Then click Insert tab, click the down arrow below Shapes, then choose a ready-made shape, in this case you can choose Oval if you want to create round photos, or choose Rounded Rectangle if you like to add round corners to a rectangle picture.


2 Answers

If you want really round corner and not only transparent trick you can use this example:

private int radius = 20;
[DefaultValue(20)]
public int Radius
{
    get { return radius; }
    set
    {
        radius = value;
        this.RecreateRegion();
    }
}

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect,
    int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);

private GraphicsPath GetRoundRectagle(Rectangle bounds, int radius)
{
    float r = radius;
    GraphicsPath path = new GraphicsPath();
    path.StartFigure();
    path.AddArc(bounds.Left, bounds.Top, r, r, 180, 90);
    path.AddArc(bounds.Right - r, bounds.Top, r, r, 270, 90);
    path.AddArc(bounds.Right - r, bounds.Bottom - r, r, r, 0, 90);
    path.AddArc(bounds.Left, bounds.Bottom - r, r, r, 90, 90);
    path.CloseFigure();
    return path;
}

private void RecreateRegion()
{
    var bounds = ClientRectangle;

    //using (var path = GetRoundRectagle(bounds, this.Radius))
    //    this.Region = new Region(path);

    //Better round rectangle
    this.Region = Region.FromHrgn(CreateRoundRectRgn(bounds.Left, bounds.Top,
        bounds.Right, bounds.Bottom, Radius, radius));
    this.Invalidate();
}

protected override void OnSizeChanged(EventArgs e)
{
    base.OnSizeChanged(e);
    this.RecreateRegion();
}

And screenshot will be:

enter image description here

The difference between this approach and making transparent:

  • Setting round region, the control has really round corners and you can see what is behind the round part despite when it is transparent, you will see background of form.
  • Setting round region, when you click on removed rounded part, click pass through the region and reaches behind, but if you use transparency trick click on transparent region will handle by the control.

You can use any of these 2 options. Making transparent or setting region based on your requirement.

Download

You can download the code or clone the repository here:

  • r-aghaei/RoundCornerControlExample
like image 165
Reza Aghaei Avatar answered Sep 17 '22 15:09

Reza Aghaei


Setting the Region makes sense only if you want to "click through" the transparent area. If the rounded corners are not so large and you want just to make the corners visually transparent, you can do the same as Button does.

The advantage of this solution that you can have a nice anti-aliased rounded corner here, while the edges of a region are always sharp. Not mentioning that a Region instance is holding unmanaged resources and should be disposed somehow.

protected override void OnPaint(PaintEventArgs e)
{
    PaintTransparentBackground(this, e);
    // TODO: Paint your actual content here with rounded corners
}

private static void PaintTransparentBackground(Control c, PaintEventArgs e)
{
    if (c.Parent == null || !Application.RenderWithVisualStyles)
        return;

    ButtonRenderer.DrawParentBackground(e.Graphics, c.ClientRectangle, c);
}
like image 28
György Kőszeg Avatar answered Sep 17 '22 15:09

György Kőszeg