Thursday, 28 March 2013

Winforms Panel with Rounded Corners

Here's a small class that can create a rounded-border effect on Winforms Panels

Please note that this is not my work, and comes from this tutorial
    public class RoundedPanel : Panel
    {
        public int Radius { get; set; }
        public Color BorderColor { get; set; }
        public Color FillColor { get; set; }
        public bool Fill { get; set; }
        public bool AntiAlias { get; set; }

        public RoundedPanel()
            : base()
        {
            BackColor = Color.White;
            FillColor = Color.Transparent;
            Fill = false;
            AntiAlias = true;
            DoubleBuffered = true;
            Radius = 18;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            GraphicsPath path = RoundedRectangle.Create(0, 0, Width - 1, Height - 1, Radius);
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.DrawPath(new Pen(BorderColor, 1f), path);
            if (Fill)
            {
                e.Graphics.FillPath(new SolidBrush(FillColor), path);
            }
        }
    }