Adjustable GDI+ Arrow Caps in VB.NET

In this article I will explain about Adjustable Arrow Caps in GDI+.
  • 2835
Adjustable arrow caps allow you to set the size of the cap's base cap, height, width, and joins. The AdjustableArrowCaps class, which is inherited from the CustomLineCap class, represents an adjustable arrow-shaped line cap. 
 
The AdjustableArrowCap class constructor takes three parameters: the width of the arrow as a floating value, the height of the arrow as a floating value and a Boolean value (optional) that, if true, indicates that the arrow cap is filled.
 
The following code snippet creates an AdjustableArrowCap object:

   Dim w As Single = 2

   Dim h As Single = 5

   Dim fill As Boolean = False

   Dim myArrow As New AdjustableArrowCap(w, h, fill)

Besides having CustomLineCap methods and properties, AdjustableArrowCap provides four properties: Filled, Height, Width, and MiddleInset. The Height and Width properties represent the height and the width, respectively of an arrow cap. The Filled property indicates whether an arrow cap is filled. The MiddleInset property represents the distance between the outline of the arrow cap and the fill.
 
Now let's add an AdjustableArrowCap option to our application. We add one menu item to the form, along with a menu item click event handler, as shown in Listing 9.8. We create two AdjustableArrowCap object and set their BasCap, BaseInset, StrokeJoin, and WidthScale properties. Then we create a black Pen object with a width of 15 and set the CustomStartCap and CustomEndCap properties of the pen as AdjustableArrowCap objects. Finally, we use this pen to draw a line with DrawLine.
LISTING 9.8: Using adjustable arrow caps
 

   Private Sub AdjustableRowCapMenu_Click(ByVal senderAs Object,ByVal e As System.EventArgs)

       ' Create a Graphics object

       Dim g As Graphics = Me.CreateGraphics()

        g.Clear(Me.BackColor)

       ' Create two AdjustableArrowCap objects

       Dim cap1 As New AdjustableArrowCap(1, 1,False)

       Dim cap2 As New AdjustableArrowCap(2, 1)

       ' Set cap properties

        cap1.BaseCap = LineCap.Round

        cap1.BaseInset = 5

        cap1.StrokeJoin = LineJoin.Bevel

        cap2.WidthScale = 3

        cap2.BaseCap = LineCap.Square

        cap2.Height = 1

       ' Create a pen

       Dim blackPen As New Pen(Color.Black, 15)

       ' Set CustomStartCap and CustomEndCap properties

        blackPen.CustomStartCap = cap1

        blackPen.CustomEndCap = cap2

       ' Draw line

        g.DrawLine(blackPen, 20, 50, 200, 50)

       ' Dispose of objects

        blackPen.Dispose()

        g.Dispose()

   End Sub

Figure 9.15 shows the output from Listing 9.8. The end caps have different sizes.

Figure209_15.jpg


FIGURE 9.15: Adjustable arrow caps
 
Conclusion
 
Hope the article would have helped you in understanding Adjustable Arrow Caps in GDI+. Read other articles on GDI+ on the website.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.