We can override the OnPaint method to draw our own border for a window, this is useful for small popup windows or a form that acts as a dialog box.
first we need to get ride of the built in Border supplied by the framework by setting:
this.FormBorderStyle = FormBorderStyle.None;
Next is to draw our forms border using the:
System.Drawing.Drawing2D.GraphicsPath();
Code:
public partial class myform : Form
{
public myform()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
}
private int h = 15;
private int w = 15;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.FormBorderStyle == FormBorderStyle.None)
{
// obtain the 2D graphics path drower
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
Rectangle r = e.ClipRectangle;
//the thickness of the border(needs to be 1, 3, 5 etc...)
Pen p = new Pen(Brushes.CornflowerBlue, 1);
gp.AddArc(r.X, r.Y, w, h, 180, 90);
gp.AddArc(r.X + r.Width - w - 1, r.Y, w, h, 270, 90);
gp.AddArc(r.X + r.Width - w - 1, r.Y + r.Height - h - 1, w, h, 0, 90);
gp.AddArc(r.X, r.Y + r.Height - h - 1, w, h, 90, 90);
gp.AddLine(r.X, r.Y + r.Height - h, r.X, r.Y + (h / 2));
System.Drawing.Drawing2D.LinearGradientBrush br = new System.Drawing.Drawing2D.LinearGradientBrush(r, Color.WhiteSmoke,
Color.Gainsboro, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
e.Graphics.FillPath(br, gp);
e.Graphics.DrawPath(p, gp);
}
}
}