Live Chat Icon For mobile
Live Chat Icon

With a wide pen, how can I control how the endpoints of my line appear

Platform: WinForms| Category: Pens

The LineCap property of the Pen class controls how the ends of your line segments appear. You can set each end independently using the StartCap and EndCap properties of the Pen class. The following code segment produces the picture below.

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
 {
   Pen redPen = new Pen(Color.LightSalmon, 20);
   int startX = 80;
   int startY = 30;
   int width = this.ClientSize.Width - 2 * startX;
 Font _font = new Font("Arial Black", 13);
 foreach(LineCap LC in new LineCap[] { LineCap.ArrowAnchor,
           LineCap.DiamondAnchor,
           LineCap.Flat,
           LineCap.Round,
           LineCap.RoundAnchor,
           LineCap.Square,
           LineCap.SquareAnchor,
           LineCap.Triangle})
   {
     redPen.StartCap = LC;
     redPen.EndCap = LC;
     Point p1 = new Point(startX, startY);
     Point p2 = new Point(startX + width, startY);
     e.Graphics.DrawLine(redPen, p1, p2); e.Graphics.DrawString( LC.ToString(), _font, new SolidBrush(Color.Blue), startX + 40, startY - 13 ); startY +=  50;
 }
 }

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.