Custom drawing using System.Windows.Forms.BorderStyle?

1k Views Asked by At

I want to mimick drawing of default border based on value of property BorderStyle. Instead of single border around the control, my control is visualised as four adjacent custom-drawn boxes (2×2), each having standard border drawn individually. So for example, if Control.Border is set to FixedSingle value I want to draw single border around each box. Simplified example:

enter image description here

I have two related problems:

  • using which standard method I can draw border from enumeration System.Windows.Forms.BorderStyle?
  • how do I determine pixel width of given border (e.g. Fixed3D) so I can use it in layout calculations?
1

There are 1 best solutions below

8
On BEST ANSWER

If you want to get results that reliably look like the BorderStyles on the machine you should make use of the methods of the ControlPaint object.

For testing let's do it ouside of a Paint event:

Panel somePanel = panel1;

using (Graphics G = somePanel.CreateGraphics())
{
    G.FillRectangle(SystemBrushes.Window, new Rectangle(11, 11, 22, 22));
    G.FillRectangle(SystemBrushes.Window, new Rectangle(44, 44, 66, 66));
    G.FillRectangle(SystemBrushes.Window, new Rectangle(11, 44, 22, 66));
    G.FillRectangle(SystemBrushes.Window, new Rectangle(44, 11, 66, 22));

    ControlPaint.DrawBorder3D(G, new Rectangle(11, 11, 22, 22));
    ControlPaint.DrawBorder3D(G, new Rectangle(44, 44, 66, 66));
    ControlPaint.DrawBorder3D(G, new Rectangle(11, 44, 22, 66));
    ControlPaint.DrawBorder3D(G, new Rectangle(44, 11, 66, 22));
}

Console.WriteLine("BorderSize.Width = " + SystemInformation.BorderSize.Width);
Console.WriteLine("Border3DSize.Width = " + SystemInformation.Border3DSize.Width);

On my machine this results in a screenshot similar to yours..:

enter image description here

..and these lines in the output:

BorderSize.Width = 1

Border3DSize.Width = 2