.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Working With PenCaps

Posted By:Abhisek Panda       Posted Date: November 25, 2009    Points: 10    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

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.


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend