Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a rounded corners Panel and Clip bound

Tags:

c#

winforms

I'm trying to draw a border around panel.But I have the problem as below: enter image description here
the border doesn't clip.
My code:

protected override void OnPaint(PaintEventArgs e)
{
       base.OnPaint(e);
       int diameter = radius * 2;
       Size size = new Size(diameter, diameter);
       int w = size.Width-1;
       int h = size.Height-1;
       Rectangle arc = new Rectangle(bounds.Location.X, bounds.Location.Y, w, h);   
       GraphicsPath path = new GraphicsPath(); 
       path.AddArc(arc, 180, 90);
       arc.X = bounds.Right - diameter;
       path.AddArc(arc, 270, 90);
       arc.Y = bounds.Bottom- diameter;
       path.AddArc(arc, 0, 90);
       arc.X = bounds.Left;
       path.AddArc(arc, 90, 90);
       path.CloseFigure(); 
       GraphicsPath GraphPath = path;
       this.Region = new Region(GraphPath);
       using (Pen pen = new Pen(Color.Blue, 1)) {
         e.Graphics.DrawPath(pen, path);
       }
}
like image 713
Jandy Avatar asked Jan 18 '26 03:01

Jandy


1 Answers

You can make the panel's BackColor transparent and then draw the Background yourself like this:

e.Graphics.FillPath(Brushes.LightBlue, path);

right before you draw the border.
However the transparency of WinForms isn't really perfect, the corners will just take the BackColor of the parent control, so if you are planning to draw anything underneath it, they will still cover it.

like image 127
TheCodingDamian Avatar answered Jan 20 '26 18:01

TheCodingDamian