There are several ways to do this.
Here is a simple one, which may or may not be good enough for you.
Let's assume an adorning Image img
and a border width of int bw
.
int iw2 = img.Width / 2;
int ih2 = img.Height / 2;
Start by setting a Padding
:
adornedPanel1.Padding = new Padding(bw, ih2 + bw, iw2 + bw, bw );
This will not show but will help keep any docked controls inside your border.
Next we code the Paint/OnPaint
code:
Rectangle cr = adornedPanel1.ClientRectangle;
Rectangle r = new Rectangle(0, ih2, cr.Width - iw2, cr.Height - ih2 );
using (Pen pen = new Pen(Color.MediumSlateBlue, bw)
{ Alignment = System.Drawing.Drawing2D.PenAlignment.Inset})
e.Graphics.DrawRectangle(pen, r);
e.Graphics.DrawImage(img, cr.Right - img.Width, 0);

To show how the Padding
works for docked and/or anchored controls the example has a Label
with a solid BackColor
docked to the left nested inside the Panel
. The right screenshot shows the Panel
with a white background so you can see the whole layout.
Note the setting of the Pen alignment from Center
to Inset
!!
If you create a panel subclass I suggest it keeps track of the new custom client area rectangle..
Other solutions could be:
- Create a shaped panel using a region
- Overlaying another Panel/Label/PictureBox