|Title||Draw lines with custom start and end caps in VB .NET|
|Description||This example shows how to draw lines with custom start and end caps in VB .NET. The program makes sets its Pen's CustomStartCap and CustomEndCap properties to CustomLineCap objects.|
|Keywords||line caps, start cap, end cap, StartCap, EndCap|
The program creates a GraphicsPath object to define the path that should be drawn to produce the start cap. It adds two lines to draw the tail of an arrow. The coordinate system has X increasing to the left of the line and Y increasing in the line's direction, with (0, 0) at the line's end point. The program uses the GraphicsPath to create a CustomLineCap object.
The program repeats these steps to make the end cap that looks like the head of an arrow.
Then the program creates a Pen and sets its CustomStartCap and CustomEndCap properties to the CustomLineCap objects. Finally it draws some lines.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs) Handles _
' Make a GraphicsPath to define the start cap.
Dim start_path As New GraphicsPath
start_path.AddLine(0, 0, 2, 2)
start_path.AddLine(0, 0, -2, 2)
' Make the start cap.
Dim start_cap As New CustomLineCap(Nothing, start_path)
' Make a GraphicsPath to define the end cap.
Dim end_path As New GraphicsPath
end_path.AddLine(0, 0, -2, -2)
end_path.AddLine(0, 0, 2, -2)
' Make the end cap.
Dim end_cap As New CustomLineCap(Nothing, end_path)
' Make a pen that uses the custom caps.
Dim the_pen As New Pen(Color.Blue, 5)
the_pen.CustomStartCap = start_cap
the_pen.CustomEndCap = end_cap
' Draw some lines.
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
e.Graphics.DrawLine(the_pen, 40, 40, 200, 40)
e.Graphics.DrawLine(the_pen, 40, 80, 200, 200)
e.Graphics.DrawLine(the_pen, 20, 100, 60, 250)
' Clean up.