I'm trying to draw a border around panel.But I have the problem as below:
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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With