Pen dash style


   

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  public class Form1 : System.Windows.Forms.Form
  {
    private System.ComponentModel.Container components;

    public Form1()
    {
      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.Form1_Paint);

    }
    [STAThread]
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void Form1_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);
    }
  }



           
          


Draw Polygon with dashed pen


   

  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;
      // Draw a pruple dashed polygon as well...
      Pen pen3 = new Pen(Color.Purple, 5);
      pen3.DashStyle = DashStyle.DashDotDot;

      g.DrawPolygon(pen3, new Point[]{new Point(30, 140),
                         new Point(265, 200),
                         new Point(100, 225),
                         new Point(190, 190),
                         new Point(50, 330),
                         new Point(20, 180)} );

    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
       Invalidate();  
    }
  }


           
          


Pen dash pattern


   


  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;
      g.FillRectangle(Brushes.White, this.ClientRectangle);
      Pen p = new Pen(Color.Black, 1);

      // The following line creates the custom dash pattern:
      float[] f = { 15, 5, 10, 5 };

      p.DashPattern = f;
      g.DrawRectangle(p, 10, 10, 80, 100);
      p.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


           
          


Pen Dash style: dash


   



  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;
      g.FillRectangle(Brushes.White, this.ClientRectangle);
      Pen p = new Pen(Color.Black, 1);

      p.DashStyle = DashStyle.Dash;
      g.DrawLine(p, 3, 3, 100, 3);
      p.Dispose();


    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }

           
          


All dash styles


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Drawing2D;

public class Form1 : Form
{

      public Form1() {
            InitializeComponent();
            
      }
    private void SimpleStyleRenderer_Paint(object sender, PaintEventArgs e)
    {
      Pen myPen = new Pen(Color.Blue, 10);
      int y = 20;
      foreach (DashStyle dash in 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 InitializeComponent()
    {
      this.SuspendLayout();
      // 
      // SimpleStyleRenderer
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(384, 353);
      this.Name = "SimpleStyleRenderer";
      this.Text = "SimpleStyleRenderer";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.SimpleStyleRenderer_Paint);
      this.ResumeLayout(false);

    }
      [STAThread]
      static void Main()
      {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
      }

}


           
          


Click on the form to draw curve


   


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{
   private Point[] thePoints;
   private int counter;

   public Form1() {
      Console.WriteLine("click on the form to draw curve");
    
    
      InitializeComponent();
      thePoints = new Point[1000];
      for (int i=0;i<thePoints.Length; i++)
         thePoints&#91;i&#93; = new Point(0,0);
      counter =0;
   }
   protected override void OnPaint (PaintEventArgs e) {
      if (counter==0) 
         return;

      Pen redPen   = new Pen(Color.Red, 3);
      Pen greenPen = new Pen(Color.Red, 4);
      Point&#91;&#93; curvePoints = new Point&#91;counter&#93;;
      
      for (int i=0; i<curvePoints.Length;i++)
      {
         curvePoints&#91;i&#93; = new Point(0,0);
         curvePoints&#91;i&#93;.X = thePoints&#91;i&#93;.X;
         curvePoints&#91;i&#93;.Y = thePoints&#91;i&#93;.Y;
      }
        
      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
      e.Graphics.DrawCurve(greenPen, curvePoints);
   }

   private void InitializeComponent() {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Name = "Form1";
      this.Text = "Form1";
      this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
   }

   static void Main() {
      Application.Run(new Form1());
   }

   private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
      thePoints&#91;counter&#93;.X = e.X;
      thePoints&#91;counter&#93;.Y = e.Y;
      Label l = new Label();
      this.SuspendLayout();

      counter++;
      if (counter>4)
        this.Refresh();
  }
}
           
          


Draw closed curve


   


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{
   private Point[] thePoints;
   private int counter;

   public Form1() {
      Console.WriteLine("click on the form to draw closed curve.");
    
    
      InitializeComponent();
      thePoints = new Point[1000];
      for (int i=0;i<thePoints.Length; i++)
         thePoints&#91;i&#93; = new Point(0,0);
      counter =0;
   }
   protected override void OnPaint (PaintEventArgs e) {
      if (counter==0) 
         return;

      Pen redPen   = new Pen(Color.Red, 3);
      Pen greenPen = new Pen(Color.Red, 4);
      Point&#91;&#93; curvePoints = new Point&#91;counter&#93;;
      
      for (int i=0; i<curvePoints.Length;i++)
      {
         curvePoints&#91;i&#93; = new Point(0,0);
         curvePoints&#91;i&#93;.X = thePoints&#91;i&#93;.X;
         curvePoints&#91;i&#93;.Y = thePoints&#91;i&#93;.Y;
      }
        
      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
      e.Graphics.DrawClosedCurve(greenPen, curvePoints);
   }

   private void InitializeComponent() {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Name = "Form1";
      this.Text = "Form1";
      this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
   }

   static void Main() {
      Application.Run(new Form1());
   }

   private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
      thePoints&#91;counter&#93;.X = e.X;
      thePoints&#91;counter&#93;.Y = e.Y;
      Label l = new Label();
      this.SuspendLayout();

      counter++;
      if (counter>4)
        this.Refresh();
  }
}