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 lines with custom start and end caps in VB .NET
DescriptionThis 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.
Keywordsline caps, start cap, end cap, StartCap, EndCap
CategoriesVB.NET, Graphics
 
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 _
    MyBase.Paint
    ' 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.
    the_pen.Dispose()
    start_path.Dispose()
    start_cap.Dispose()
    end_path.Dispose()
    end_cap.Dispose()
End Sub
 
My next book includes a lot more information on Pens, Brushes, and other drawing objects in VS 2005. Check the VB Helper Web site or sign up for my newsletter to learn more about this book when it is available.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated