Thursday, November 15, 2007

How to creating a larger arrowhead when drawing lines in VB.NET

To attach an arrow to the end of a line in VB.NET, you just need to do
the following:

Dim aPen as Pen = New Pen(Color.Red)
aPen.EndCap = LineCap.ArrowAnchor

Then when you draw the line, you specify the pen created above as the
pen for the line. The problem with this approach is that the size of
the arrow depends on the size of the line, in this case, our line is
of pixel width = 1, if we create a larger line we will have a larger
arrow, but what if you don't want a larger line, but do want a larger
arrowhead (the arrowhead with a line of 1 pixel is so small that its
hard to even see it).

Here we create a Custom Cap. Below is the code:
Dim aPen as Pen = New Pen(Color.Red)
Dim mycap As CustomLineCap = New AdjustableArrowCap(5, 5)
aPen.EndCap = LineCap.ArrowAnchor
aPen.CustomEndCap = mycap

Now the arrowhead will be 5 pixel by 5 pixels, which is a decent enough size.

No comments: