Csharp/CSharp Tutorial/2D/Pens
Содержание
A custom dashed Pen
<source lang="csharp">using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class PenDashedDot : System.Windows.Forms.Form {
private System.ruponentModel.Container components = null; public PenDashedDot() { InitializeComponent(); SetStyle(ControlStyles.ResizeRedraw, true); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Paint += new System.Windows.Forms.PaintEventHandler(this.PenDashedDot_Paint); } [STAThread] static void Main() { Application.Run(new PenDashedDot()); } private void PenDashedDot_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Pen customDashPen = new Pen(Color.BlueViolet, 5); float[] myDashes = {5.0f, 2.0f, 1.0f, 3.0f}; customDashPen.DashPattern = myDashes; g.DrawRectangle(customDashPen, ClientRectangle); }
}</source>
All LineCap Demo
<source lang="csharp">using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class LineCapDemo : System.Windows.Forms.Form {
private System.ruponentModel.Container components = null; public LineCapDemo() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Resize += new System.EventHandler(this.LineCapDemo_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.LineCapDemo_Paint); } [STAThread] static void Main() { Application.Run(new LineCapDemo()); } private void LineCapDemo_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Pen thePen = new Pen(Color.Black, 10); int yOffSet = 10; Array obj = Enum.GetValues(typeof(LineCap)); for(int x = 0; x < obj.Length; x++) { LineCap temp = (LineCap)obj.GetValue(x); thePen.StartCap = temp; thePen.EndCap = temp; g.DrawString(temp.ToString(), new Font("Times New Roman", 10), new SolidBrush(Color.Black), 0, yOffSet); g.DrawLine(thePen, 100, yOffSet, Width - 50, yOffSet); yOffSet += 40; } } private void LineCapDemo_Resize(object sender, System.EventArgs e) { Invalidate(); }
}</source>
Dashed pen
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class PenCustomDash : System.Windows.Forms.Form {
private System.ruponentModel.Container components = null; public PenCustomDash() { InitializeComponent(); SetStyle(ControlStyles.ResizeRedraw, true); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(211, 104); this.Text = "A Form with Style!"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.PenCustomDash_Paint); } [STAThread] static void Main() { Application.Run(new PenCustomDash()); } private void PenCustomDash_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Pen customDashPen = new Pen(Color.Black, 10); float[] myDashes = {5.0f, 2.0f, 1.0f, 3.0f}; customDashPen.DashPattern = myDashes; e.Graphics.DrawRectangle(customDashPen, ClientRectangle); }
}</source>
Disposal Errors
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Windows.Forms;
class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { Pen p = Pens.Black; p.Dispose(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 271); this.Name = "Form1"; this.Text = "Form1"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } }</source>
LineCap.ArrowAnchor
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form {
[STAThread] static void Main() { Application.Run(new Form1()); } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Pen p = new Pen(Color.Brown, 15); // set the Arrow p.StartCap = LineCap.ArrowAnchor; p.EndCap = LineCap.ArrowAnchor; g.DrawLine(p, 30, 30, Width - 50, 30); }
}</source>
LineCap.Round
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form {
[STAThread] static void Main() { Application.Run(new Form1()); } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Pen p = new Pen(Color.Brown, 15); // round ends p.StartCap = LineCap.Round; p.EndCap = LineCap.Round; g.DrawLine(p, 30, 80, Width - 50, 80); }
}</source>
LineCap.RoundAnchor
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form {
[STAThread] static void Main() { Application.Run(new Form1()); } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Pen p = new Pen(Color.Brown, 15); // round Anchor p.StartCap = LineCap.RoundAnchor; p.EndCap = LineCap.RoundAnchor; g.DrawLine(p, 30, 120, Width - 50, 120); }
}</source>
LineCap.SquareAnchor
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form {
[STAThread] static void Main() { Application.Run(new Form1()); } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Pen p = new Pen(Color.Brown, 15); // square Anchor p.StartCap = LineCap.SquareAnchor; p.EndCap = LineCap.SquareAnchor; g.DrawLine(p, 30, 190, Width - 50, 190); }
}</source>
LineCap.Triangle
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D; public class Form1 : System.Windows.Forms.Form {
[STAThread] static void Main() { Application.Run(new Form1()); } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Pen p = new Pen(Color.Brown, 15); // triangle p.StartCap = LineCap.Triangle; p.EndCap = LineCap.Triangle; g.DrawLine(p, 30, 150, Width - 50, 150); }
}</source>
Make a big blue pen
<source lang="csharp">using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class PenBigBlue : System.Windows.Forms.Form {
private System.ruponentModel.Container components = null; public PenBigBlue() { InitializeComponent(); SetStyle(ControlStyles.ResizeRedraw, true); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Paint += new System.Windows.Forms.PaintEventHandler(this.PenBigBlue_Paint); } [STAThread] static void Main() { Application.Run(new PenBigBlue()); } private void PenBigBlue_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; // Make a big blue pen. Pen bluePen = new Pen(Color.Blue, 20); g.DrawEllipse(bluePen, 10, 10, 100, 100); }
}</source>
PenAlignment.Inset
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms; public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Pen p = new Pen(Color.Black, 3); p.Alignment = PenAlignment.Inset; g.DrawRectangle(p, 3, 3, 8, 7); p.Dispose(); } public static void Main() { Application.Run(new Form1()); }
}</source>
Pens: Firebrick
<source lang="csharp">using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class PensFirebrick : System.Windows.Forms.Form {
private System.ruponentModel.Container components = null; public PensFirebrick() { InitializeComponent(); SetStyle(ControlStyles.ResizeRedraw, true); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Paint += new System.Windows.Forms.PaintEventHandler(this.PensFirebrick_Paint); } [STAThread] static void Main() { Application.Run(new PensFirebrick()); } private void PensFirebrick_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Pen pen2 = Pens.Firebrick; g.DrawLine(pen2, 10, 130, 110, 130); }
}</source>
Pen start and end cap
<source lang="csharp">using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class PenStartEndCap : System.Windows.Forms.Form {
public PenStartEndCap() { this.BackColor = System.Drawing.Color.White; this.ClientSize = new System.Drawing.Size(400, 400); this.Paint += new System.Windows.Forms.PaintEventHandler(this.PenStartEndCap_Paint); }
static void Main() { Application.Run(new PenStartEndCap()); } private void PenStartEndCap_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Pen p2 = new Pen(Color.Gray, 7); p2.EndCap = LineCap.Round; p2.StartCap = LineCap.ArrowAnchor; g.DrawLine(p2, 25, 35, 25, 365); g.DrawLine(p2, 35, 375, 365, 375); g.DrawLine(p2, 375, 365, 375, 35); g.DrawLine(p2, 365, 25, 35, 25); }
}</source>
Use Pen directly
<source lang="csharp">using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class PensDirectly : System.Windows.Forms.Form {
private System.ruponentModel.Container components = null; public PensDirectly() { InitializeComponent(); SetStyle(ControlStyles.ResizeRedraw, true); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Paint += new System.Windows.Forms.PaintEventHandler(this.PensDirectly_Paint); } [STAThread] static void Main() { Application.Run(new PensDirectly()); } private void PensDirectly_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.DrawPie(Pens.Black, 150, 10, 120, 150, 90, 80); }
}</source>