比派克还派克
参考:
“GDI+编程10个基本技巧”
"橡皮筋"
用钢笔在画布上画出虚线
要在Graphics对象上画出虚线,以及虚线边框的几何形状,关键全在Pen对象上。
Pen.DashStyle——用于设置虚线样式。该属性接收一个DashStyle枚举。
Dash |
点画线 |
---|---|
DashDot |
点画线点 |
DashDotDot |
点画线点点 |
Dot |
点 |
Solid |
实线 |
Custom |
用户自定义的线型 |
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Pen p = new Pen(Color.Red);
p.DashStyle = DashStyle.Dash;
e.Graphics.DrawLine(p, new Point(10, 10), new Point(300, 300));
}
Pen.DashCap——短划线终点的线相样式。该属性接受一个Dash枚举。
Flat |
两端均为方形的方相。 |
---|---|
Round |
两端均为圆角的圆相。 |
Triangle |
两端均为带三角的三角相。 |
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
// Set the SmoothingMode property to smooth the line.
e.Graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// Create a new Pen object.
Pen greenPen = new Pen(Color.Green);
// Set the width to 6.
greenPen.Width = 6.0F;
// Set the DashCap to round.
greenPen.DashCap = System.Drawing.Drawing2D.DashCap.Round;
// Create a custom dash pattern.
greenPen.DashPattern = new float[]{4.0F, 2.0F, 1.0F, 3.0F};
// Draw a line.
e.Graphics.DrawLine(greenPen, 20.0F, 20.0F, 100.0F, 240.0F);
// Change the SmoothingMode to none.
e.Graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.None;
// Draw another line.
e.Graphics.DrawLine(greenPen, 100.0F, 240.0F, 160.0F, 20.0F);
// Dispose of the custom pen.
greenPen.Dispose();
}
Pen.DashOffset——直线的起点到短划线图案起始处的距离。
Pen.DashPattern——自定义的短划线和空白区域的数组。
给直线画上箭头
Pen.StartCap——直线起点的线相样式。
Pen.EndCap——直线终点的线相样式。
这两个属性都接受LineCap枚举。
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Pen p = new Pen(Color.Red);
p.Width = 20;
p.StartCap = LineCap.RoundAnchor;
p.EndCap = LineCap.DiamondAnchor;
e.Graphics.DrawLine(p, 50, 50, 250, 250);
}
Pen.CustomStartCap——自定义的直线段起点线相样式。
Pen.CustomEndCap——自定义的直线段终点线相样式。
这两个属性接受CustomLineCap对象。
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
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));
// Construct the hook-shaped end cap.
CustomLineCap HookCap = new CustomLineCap(null, hPath);
// 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, 5);
customCapPen.CustomStartCap = HookCap;
customCapPen.CustomEndCap = HookCap;
// Create a second pen using the start and end caps from
// the hook cap.
Pen capPen = new Pen(Color.Red, 10);
LineCap startCap;
LineCap endCap;
HookCap.GetStrokeCaps(out startCap, out endCap);
capPen.StartCap = startCap;
capPen.EndCap = endCap;
// Create a line to draw.
Point[] points = { new Point(100, 100), new Point(200, 50),
new Point(250, 300) };
// Draw the lines.
e.Graphics.DrawLines(capPen, points);
e.Graphics.DrawLines(customCapPen, points);
}
参考:
“GDI+编程10个基本技巧”
"橡皮筋"