2

I am using C# and winform application .net Version 3.5 and Vs 2008

How Would i Can create A custom Panel with the rounded edges ? How Can we use that Control in Different Project ?

Andre Luus
  • 3,692
  • 3
  • 33
  • 46
siddharth
  • 441
  • 3
  • 11
  • 20
  • 2
    on another note...a groupbox with no text has almost always worked for me when I wanted a rounded panel. The only exception being the annoying title area above the top line, but usually that is taken care of by z-ordering and the gap between any other control above it... – Arif Eqbal Jul 18 '12 at 11:54

4 Answers4

2

Following MLablanc's link I converted the code to C#:

public class RoundedPanel : Panel {
    private float _thickness = 5;
    public float Thickness {
        get {
            return _thickness;
        }
        set {
            _thickness = value;
            _pen = new Pen(_borderColor,Thickness);
            Invalidate();
        }
    }

    private Color _borderColor = Color.White;
    public Color BorderColor {
        get {
            return _borderColor;
        }
        set {
            _borderColor = value;
            _pen = new Pen(_borderColor,Thickness);
            Invalidate();
        }
    }

    private int _radius = 20;
    public int Radius {
        get {
            return _radius;
        }
        set {
            _radius = value;
            Invalidate();
        }
    }

    private Pen _pen;

    public MpRoundedPanel() : base() {
        _pen = new Pen(BorderColor,Thickness);
        DoubleBuffered = true;            
    }
    private Rectangle GetLeftUpper(int e) {
        return new Rectangle(0,0,e,e);
    }
    private Rectangle GetRightUpper(int e) {
        return new Rectangle(Width - e,0,e,e);
    }
    private Rectangle GetRightLower(int e) {
        return new Rectangle(Width - e,Height - e,e,e);
    }
    private Rectangle GetLeftLower(int e) {
        return new Rectangle(0,Height - e,e,e);
    }

    private void ExtendedDraw(PaintEventArgs e) {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        GraphicsPath path = new GraphicsPath();
        path.StartFigure();
        path.AddArc(GetLeftUpper(Radius),180,90);
        path.AddLine(Radius,0,Width - Radius,0);
        path.AddArc(GetRightUpper(Radius),270,90);
        path.AddLine(Width,Radius,Width,Height - Radius);
        path.AddArc(GetRightLower(Radius),0,90);
        path.AddLine(Width - Radius,Height,Radius,Height);
        path.AddArc(GetLeftLower(Radius),90,90);
        path.AddLine(0,Height - Radius,0,Radius);
        path.CloseFigure();
        Region = new Region(path);
    }
    private void DrawSingleBorder(Graphics graphics) {
        graphics.DrawArc(_pen,new Rectangle(0,0,Radius,Radius),180,90);
        graphics.DrawArc(_pen,new Rectangle(Width - Radius - 1,-1,Radius,Radius),270,90);
        graphics.DrawArc(_pen,new Rectangle(Width - Radius - 1,Height - Radius - 1,Radius,Radius),0,90);
        graphics.DrawArc(_pen,new Rectangle(0,Height - Radius - 1,Radius,Radius),90,90);
        graphics.DrawRectangle(_pen,0.0f,0.0f,(float)Width - 1.0f,(float)Height - 1.0f);
    }
    private void Draw3DBorder(Graphics graphics) {
        DrawSingleBorder(graphics);
    }
    private void DrawBorder(Graphics graphics) {
        DrawSingleBorder(graphics);
    }
    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);
        ExtendedDraw(e);
        DrawBorder(e.Graphics);
    }
}

To use just include this class in your project or in the same file you are declaring a Panel and use RoundedPanel instead.

tkefauver
  • 491
  • 5
  • 19
1

You have to override the OnPaint event and draw the corners using a GraphicPath object.

Take a look at this article : http://www.switchonthecode.com/tutorials/csharp-creating-rounded-rectangles-using-a-graphics-path

MLeblanc
  • 1,816
  • 12
  • 21
0

You can hide panel's edges and draw new ones with rounded corners using GDI+.

Here's an example.

Nickon
  • 9,652
  • 12
  • 64
  • 119
-1

A quick google on

winforms create A custom Panel with the rounded edges

returned the following as the first result:

C# Form with custom border and rounded edges

To use it in other projects, create the panel as a UserControl, rather than a window.

Community
  • 1
  • 1
DaveDev
  • 41,155
  • 72
  • 223
  • 385
  • 1
    "Kri-thick" Like how you pronounce the first part of "Cryptic". I made it up; it used to be Krynn "Krin", but it evolved and changed, much like myself. – Krythic Oct 05 '15 at 02:58