1 使用預定義形狀
畫筆屬性pen.StartCap和pen.EndCap指定線條的兩端形狀。
預定義的形狀:LineCap.Round 、LineCap.ArrowAnchor等等
如: pen.StartCap = LineCap.Round;
2 使用自訂形狀
如果使用自訂的端部形狀,則使用pen.CustomStartCap和pen.CustomEndCap屬性
CustomLineCap myCap = new CustomLineCap(null,path);//自訂端部形狀
path為GraphicsPath類型。
如:pen.CustomStartCap = mycap;
樣本:
Code
GraphicsPath hPath = new GraphicsPath();
// Create the outline for our custom end cap.
hPath.AddLine(new Point(0, 0), new Point(0, 5));
hPath.AddLine(new Point(0, 5), new Point(5, 1));
hPath.AddLine(new Point(5, 1), new Point(3, 1));
//構造一個掛鈎形狀的端部形狀
CustomLineCap HookCap = new CustomLineCap(null, hPath);
// 設定畫筆線條端部形狀為它
Pen customCapPen = new Pen(Color.Black, 5);
customCapPen.CustomStartCap = HookCap;
customCapPen.CustomEndCap = HookCap;
//使用預定義的端部形狀
Pen capPen = new Pen(Color.Red, 10);
capPen.StartCap = LineCap.Round;
capPen.EndCap = LineCap.ArrowAnchor;
//無端部形狀
Pen pen = new Pen(Color.Chartreuse, 7);
// Create a line to draw.
Point[] points = { new Point(100, 100), new Point(300, 100) };
// Draw the lines.
e.Graphics.DrawLines(capPen, points);
e.Graphics.DrawLines(pen,points);
e.Graphics.DrawLines(customCapPen, points);