using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Pen p = new Pen(Color.Black, 1); float[] f = { 15, 5, 10, 5 }; p.DashPattern = f; g.DrawRectangle(p, 10, 10, 80, 100); p.Dispose(); } public static void Main() { Application.Run(new Form1()); } }
StartCap, EndCap
using System; using System.Drawing; using System.Collections; using System.ComponentModel; 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); p.StartCap = LineCap.SquareAnchor; p.EndCap = LineCap.SquareAnchor; g.DrawLine(p, 30, 190, Width - 50, 190); } }
Dispose a pen
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Pen p = new Pen(Color.Black); g.DrawLine(p, 0, 0, 100, 100); p.Dispose(); } public static void Main() { Application.Run(new Form1()); } }
illustrates the use of multiple Pens
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example21_2.cs illustrates the use of multiple Pens */ using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class Example21_2 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; public Example21_2() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.BackColor = System.Drawing.Color.White; this.ClientSize = new System.Drawing.Size(400, 400); this.Name = "Example21_2"; this.Text = "Example21_2"; this.Paint += new System.Windows.Forms. PaintEventHandler(this.Example21_2_Paint); } static void Main() { Application.Run(new Example21_2()); } private void Example21_2_Paint( object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; // draw two lines with one pen Pen p = new Pen(Color.Black, 10); g.DrawLine(p, 25, 25, 375, 375); g.DrawLine(p, 25, 375, 375, 25); // draw four lines with another pen 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); } }
Pen Dash Styles
/* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace GDI_Basics { /// <summary> /// Summary description for PenDashStyles. /// </summary> public class PenDashStyles : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public PenDashStyles() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { // // PenDashStyles // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(500, 398); this.Name = "PenDashStyles"; this.Text = "PenDashStyles"; this.Resize += new System.EventHandler(this.PenDashStyles_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.PenDashStyles_Paint); } #endregion [STAThread] static void Main() { Application.Run(new PenDashStyles()); } private void PenDashStyles_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Pen myPen = new Pen(Color.Blue, 10); int y = 20; foreach (DashStyle dash in System.Enum.GetValues(typeof(DashStyle))) { myPen.DashStyle = dash; e.Graphics.DrawLine(myPen, 20, y, 100, y); e.Graphics.DrawString(dash.ToString(), new Font("Tahoma", 8), Brushes.Black, 120, y - 10); y += 30; } y += 10; myPen.StartCap = LineCap.Round; myPen.EndCap = LineCap.Round; foreach (DashStyle dash in System.Enum.GetValues(typeof(DashStyle))) { myPen.DashStyle = dash; e.Graphics.DrawLine(myPen, 20, y, 100, y); e.Graphics.DrawString(dash.ToString() + " (with round caps)", new Font("Tahoma", 8), Brushes.Black, 120, y - 10); y += 30; } } private void PenDashStyles_Resize(object sender, System.EventArgs e) { this.Invalidate(); } } }
Pen width and color
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class MainForm : System.Windows.Forms.Form { public MainForm() { InitializeComponent(); CenterToScreen(); SetStyle(ControlStyles.ResizeRedraw, true); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = "Pens..."; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint); } static void Main() { Application.Run(new MainForm()); } private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Pen bluePen = new Pen(Color.Blue, 20); Pen pen2 = Pens.Firebrick; // Render some shapes with the pens. g.DrawEllipse(bluePen, 10, 10, 100, 100); g.DrawLine(pen2, 10, 130, 110, 130); g.DrawPie(Pens.Black, 150, 10, 120, 150, 90, 80); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } }
Pen alignment: inset
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Pen p = new Pen(Color.Black, 3); p.Alignment = PenAlignment.Inset; g.DrawRectangle(p, 3, 3, 80, 70); p.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } }