Draw 2D and 3D borders in C#

2D and 3D borders

This example uses the following DrawBorder method to draw 2D and 3D borders inside the edges of Rectangles.

private void DrawBorder(Graphics gr, Rectangle rect,
    BorderStyle border_style, bool sunken)
{
    if (border_style == BorderStyle.FixedSingle)
    {
        rect.Width -= 1;
        rect.Height -= 1;
        gr.DrawRectangle(Pens.Black, rect);
    }
    else if (border_style == BorderStyle.Fixed3D)
    {
        Color[] colors;
        if (sunken)
        {
            colors = new Color[]
            {
                SystemColors.ControlDark,
                SystemColors.ControlDarkDark,
                SystemColors.ControlLightLight,
                SystemColors.ControlLight
            };
        }
        else
        {
            colors = new Color[]
            {
                SystemColors.ControlLightLight,
                SystemColors.ControlLight,
                SystemColors.ControlDark,
                SystemColors.ControlDarkDark
            };
        }
        using (Pen p = new Pen(colors[0]))
        {
            gr.DrawLine(p, rect.X, rect.Bottom - 1,
                rect.X, rect.Y);
            gr.DrawLine(p, rect.X, rect.Y,
                rect.Right - 1, rect.Y);
        }
        using (Pen p = new Pen(colors[1]))
        {
            gr.DrawLine(p, rect.X + 1, rect.Bottom - 2,
                rect.X + 1, rect.Y + 1);
            gr.DrawLine(p, rect.X + 1, rect.Y + 1,
                rect.Right - 2, rect.Y + 1);
        }
        using (Pen p = new Pen(colors[2]))
        {
            gr.DrawLine(p, rect.X, rect.Bottom - 1,
                rect.Right - 1, rect.Bottom - 1);
            gr.DrawLine(p, rect.Right - 1, rect.Bottom - 1,
                rect.Right - 1, rect.Y);
        }
        using (Pen p = new Pen(colors[3]))
        {
            gr.DrawLine(p, rect.X + 1, rect.Bottom - 2,
                rect.Right - 2, rect.Bottom - 2);
            gr.DrawLine(p, rect.Right - 2, rect.Bottom - 2,
                rect.Right - 2, rect.Y + 1);
        }
    }
}

If the border_style parameter is FixedSingle, the code subtracts 1 from the Rectangle‘s width and height so the border lies just inside the Rectangle. It then draws the Rectangle as a flat, 2D border.

If border_style is Fixed3D, the code checks the sunken parameter and initializes an array to the system colors it should use for the different parts of the border. It then uses the Graphics object’s DrawLine method to draw the border.

Each side of the rectangle is drawn by two lines. The dark sides have a Dark outside a DarkDark line. The lighter sides have a LightLight line outside a Light line.

(The only other value defined by the BorderStyle enumeration is None. If border_style is None, the method doesn’t draw anything.)


Download Example   Follow me on Twitter   RSS feed




This entry was posted in drawing, graphics and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *