Working With PenCaps

Posted by Abhisek under C# category on | Views : 1236
Working With PenCaps:-

Instead of using simple pens(ends with caps of 90 degree angle) in an application we can use different pen cap styles for the pen using the LineCap enumeration which defines different pen cap styles as follows,

public enum LineCap
Flat, Square, Round, Triangle, NoAncher, SquareAnchor,
RoundAnchor, DiamondAnchor, ArrowAnchor, AnchorMask, Custom

To use these pen caps just create a widow form application in C# and add the following code in the Paint event handler of the form. The code simply loops through each member of the LineCap enum and prints out the name of the item and then configures and draws a line with the current cap.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace PenCapApp
public partial class MainForm : Form
public MainForm()
private void MainForm_Paint(object sender, PaintEventArgs e)
Graphics g = e.Graphics;
Pen thePen = new Pen(Color.Black, 10);
int yOffSet = 10;
// Get all members of the LineCap enum.
Array obj = Enum.GetValues(typeof(LineCap));
// Draw a line with a LineCap member.
for (int x = 0; x < obj.Length; x++)
// Get next cap and configure pen.
LineCap temp = (LineCap)obj.GetValue(x);
thePen.StartCap = temp;
thePen.EndCap = temp;
// Print name of LineCap enum.
g.DrawString(temp.ToString(), new Font("Times New Roman", 10),
new SolidBrush(Color.Black), 0, yOffSet);
// Draw a line with the correct cap.
g.DrawLine(thePen, 100, yOffSet, Width - 50, yOffSet);
yOffSet += 40;

When you run this application it will show you the different pen cap styles with there names.

Comments or Responses

Login to post response