How to create a User Control with rounded corners?

前端 未结 3 1988
不思量自难忘°
不思量自难忘° 2020-12-06 12:09

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 Use

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 12:56

    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();
        }
    }
    private GraphicsPath GetRoundRectagle(Rectangle bounds, int radius)
    {
        GraphicsPath path = new GraphicsPath();
        path.AddArc(bounds.X, bounds.Y, radius, radius, 180, 90);
        path.AddArc(bounds.X + bounds.Width - radius, bounds.Y, radius, radius, 270, 90);
        path.AddArc(bounds.X + bounds.Width - radius, bounds.Y + bounds.Height - radius, 
                    radius, radius, 0, 90);
        path.AddArc(bounds.X, bounds.Y + bounds.Height - radius, radius, radius, 90, 90);
        path.CloseAllFigures();
        return path;
    }
    private void RecreateRegion()
    {
        var bounds = ClientRectangle;
        bounds.Width--; bounds.Height--;
        using (var path = GetRoundRectagle(bounds, this.Radius))
            this.Region = new Region(path);
        this.Invalidate();
    }
    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        this.RecreateRegion();
    }
    

    And screenshot will be:

    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

提交回复
热议问题