Adjustable Arrow Caps in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

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:

            float w = 2;
            float h = 5;
            bool fill = false;
            AdjustableArrowCap myArrow = 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 void AdjustableRowCapMenu_Click(object sender, System.EventArgs e)
        {
            // Create a Graphics object
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);

            // Create two AdjustableArrowCap objects
            AdjustableArrowCap cap1 = new AdjustableArrowCap(1, 1, false);
            AdjustableArrowCap cap2 = 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
            Pen blackPen = 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();
        }

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

Figure 9.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.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.