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

5 comments:

  1. Hi,
    You have used this code-
    RoundedRectangle.Create(0, 0, Width - 1, Height - 1, Radius);

    where this RoundedRectangle class is defined?

    ReplyDelete
  2. Oops, looks like I omitted that. Check out the link in the post-description to a tutorial where i originally found the source for this functionality

    ReplyDelete
    Replies
    1. can you copy the RoundedRectalgle
      class code cause the link won't work

      Delete
  3. not working, and with http://web.archive.org/web/20150427121927/http://tech.pro/tutorial/656/csharp-creating-rounded-rectangles-using-a-graphics-path also

    ReplyDelete