Live Chat Icon For mobile
Live Chat Icon

WinForms FAQ - Pens

Find answers for the most frequently asked questions
Expand All Collapse All

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;
 }
 }
Permalink

The LineJoin property of the Pen class allows you to specify how two lines should be joined. The following code segment produces the picture below.

  private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Pen redPen = new Pen(Color.Red, 20);
      
    int startX = 20;
    int startY = 60;
    int width = this.ClientSize.Width / 17;
    int height = this.ClientSize.Height / 2;

    foreach(LineJoin LJ in new LineJoin[] { LineJoin.Bevel,
                         LineJoin.Miter,
                         LineJoin.Round})
    {
      redPen.LineJoin = LJ;

      Point[] points = {new Point(startX, startY),
                  new Point(startX + width, startY + height),
                  new Point(startX + 2 * width, startY),
                  new Point(startX + 3 * width, startY + height),
                  new Point(startX + 4 * width, startY)};

      e.Graphics.DrawLines(redPen, points);
      e.Graphics.DrawString( LJ.ToString(), new Font('Arial Black', 13), 
        new SolidBrush(Color.Blue), startX - 5, startY - 50);
      startX += 4 * width + 40;
    }
  }

Permalink

Share with

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

Please submit your question and answer.