Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleDraw a 2D or 3D border in VB .NET
DescriptionThis example shows how to draw a 2-D or 3D border in Visual Basic .NET. The 3-D border code uses the system's colors to determine the 3D highlight and shadow colors and can draw the border sunken or raised.
Keywordsborder, draw border, 3D, three-dimensional, VB .NET
CategoriesVB.NET, Graphics
 
The DrawBorder subroutine draws a border inside the edges of a Rectangle. 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 and then draws the Rectangle.

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.

 
' Draw a border inside this rectangle.
Private Sub DrawBorder(ByVal gr As Graphics, ByVal rect As _
    Rectangle, ByVal border_style As BorderStyle, Optional _
    ByVal sunken As Boolean = True)
    Select Case border_style
        Case BorderStyle.FixedSingle
            rect.Width -= 1
            rect.Height -= 1
            gr.DrawRectangle(Pens.Black, rect)
        Case BorderStyle.Fixed3D
            Dim colors() As Color
            If sunken Then
                colors = New Color() { _
                    SystemColors.ControlDark, _
                    SystemColors.ControlDarkDark, _
                    SystemColors.ControlLightLight, _
                    SystemColors.ControlLight _
                }
            Else
                colors = New Color() { _
                    SystemColors.ControlLightLight, _
                    SystemColors.ControlLight, _
                    SystemColors.ControlDark, _
                    SystemColors.ControlDarkDark _
                }
            End If
            Dim p As Pen
            p = New Pen(colors(0))
            gr.DrawLine(p, rect.X, rect.Y + rect.Height - _
                1, rect.X, rect.Y)
            gr.DrawLine(p, rect.X, rect.Y, rect.X + _
                rect.Width - 1, rect.Y)
            p = New Pen(colors(1))
            gr.DrawLine(p, rect.X + 1, rect.Y + rect.Height _
                - 2, rect.X + 1, rect.Y + 1)
            gr.DrawLine(p, rect.X + 1, rect.Y + 1, rect.X + _
                rect.Width - 2, rect.Y + 1)
            p = New Pen(colors(2))
            gr.DrawLine(p, rect.X, rect.Y + rect.Height - _
                1, rect.X + rect.Width - 1, rect.Y + _
                rect.Height - 1)
            gr.DrawLine(p, rect.X + rect.Width - 1, rect.Y _
                + rect.Height - 1, rect.X + rect.Width - 1, _
                rect.Y)
            p = New Pen(colors(3))
            gr.DrawLine(p, rect.X + 1, rect.Y + rect.Height _
                - 2, rect.X + rect.Width - 2, rect.Y + _
                rect.Height - 2)
            gr.DrawLine(p, rect.X + rect.Width - 2, rect.Y _
                + rect.Height - 2, rect.X + rect.Width - 2, _
                rect.Y + 1)
    End Select
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated