c# 自定义线帽(CustomLineCap)的使用
(2013-01-10 22:41:34)
标签:
cgdi线帽customlinecap自定义it |
分类: C#.NET |
1.在new CustomLineCap实例时,对于CustomLineCap (GraphicsPath,
GraphicsPath)构造函数,第一个参数表示自定义线帽填充的内容的对象,可以设置成null,第二个参数表示自定义线帽轮廓的对象。
(GraphicsPath类:表示一系列相互连接的直线和曲线,无法继承此类。)
示例代码:
Pen XuXianpenHasCap=null;//定义一个Pen对象实例
XuXianpenHasCap = new Pen(System.Drawing.Color.Red,
1.0f);//初始该Pen对象实例
GraphicsPath hPath = new
GraphicsPath();//定义一个GraphicsPath对象实例,以便线帽中使用
hPath.AddLine(new Point(0, 0), new Point(0, 5));//初始线帽轮廓
hPath.AddLine(new Point(0, 5), new Point(3,
0));//特别注意:坐标系是以所画直线端点为原点,直线方向为y轴,垂直方向为x轴(有点费解)
hPath.AddLine(new Point(0, 5), new Point(-3, 0));//三点连线画出了一个箭头
CustomLineCap HookCap = new CustomLineCap(null,
hPath);//初始自定义线帽对象实例
//HookCap.WidthScale = 1.0f;//设置线帽和直线的比例
HookCap.SetStrokeCaps(LineCap.Round,
LineCap.Round);//设置用于构成此自定义线帽的起始直线和结束直线的线帽(似乎不能是LineCap.ArrowAnchor)
XuXianpenHasCap.StartCap =
LineCap.Flat;//设置Pen对象实例的开始端线帽,为系统定义线帽
XuXianpenHasCap.CustomEndCap = HookCap;//设置Pen对象实例的开始端线帽,为自定义线帽
g.DrawLine(XuXianpenHasCap, x2, y2, (int)((x + x2) / 2), (int)((y + y2) / 2));//使用该Pen实例画线
画填充的线帽时,根据MSDN的CustomLineCap文档,文档说FillPath必须和Y轴交叉过。【太坑爹了!】
protect override void OnPaint(PaintEventArgs
{
Graphics g = e.Graphics ;
GraphicsPath hPath = new GraphicsPath();
// Create the outline for our custom end cap.
hPath.AddLine(new Point(0, -1), new Point(3, -1));
hPath.AddLine(new Point(3, -1), new Point(0, 15));
hPath.AddLine(new Point(0, 15), new Point(-3, -1));
hPath.CloseFigure();
// Construct the hook-shaped end cap.
CustomLineCap HookCap = new CustomLineCap(hPath, null); //<---------
// Set the start cap and end cap of the HookCap to be rounded.
HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
// Create a pen and set end custom start and end
// caps to the hook cap.
Pen customCapPen = new Pen(Color.Black, 10);
customCapPen.CustomStartCap = HookCap;
customCapPen.CustomEndCap = HookCap;
// Create a line to draw.
Point[] points = { new Point(100, 100), new Point(200, 50), new Point(250, 300) };
// Draw the lines.
g.DrawLines(customCapPen, points);
g.Dispose();
}
加载中…