Pen alignment: center

image_pdfimage_print


   



  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.Center;
      g.DrawRectangle(p, 3, 3, 80, 70);
      p.Dispose();
    }

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

           
          


make red and blue pens

image_pdfimage_print


   

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

public class TestGDI1 : System.Windows.Forms.Form{
    
    //in order to paint something OnPaint method needs to be overridden
    
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe) {
        //OnPaint method is a member of Form class 
        //The following call sends pe to an event listener Graphics
        base.OnPaint(pe);
        
        //initialize Graphics 
        System.Drawing.Graphics g=pe.Graphics;
        
        //designate the area of the form where the drawing must take place
        //ClientRectangle is a member of Windows.Forms.Control class
        System.Drawing.Rectangle client_area=this.ClientRectangle;
        
        //point11 is at the top left corner of the client_area
        System.Drawing.Point point11=new System.Drawing.Point(client_area.X,client_area.Y); 
        //point12 is at the bottom right corner of the client area
        
        System.Drawing.Point point12=new System.Drawing.Point(client_area.Width,client_area.Height);
        
        //create a Brush object of white color
        //SolidBrush means that the color does not change from point to point
        System.Drawing.Brush background=new System.Drawing.SolidBrush(System.Drawing.Color.White);
        
        //color client_area with solid white brush
        g.FillRectangle(background,client_area);
        
        //make red and blue pens
        System.Drawing.Pen p=new System.Drawing.Pen(System.Drawing.Color.Red);
        System.Drawing.Pen p1=new System.Drawing.Pen(System.Drawing.Color.Blue);
        
        //create points and rectangles
        System.Drawing.SizeF size=new System.Drawing.SizeF();
        size.Height=160;
        size. Width=180;
        System.Drawing.PointF point=new System.Drawing.PointF();
        point.X=8;
        point.Y=40;
        System.Drawing.Point point1=new System.Drawing.Point();
        point1.X=300;
        point1.Y=300;
        System.Drawing.Point point2=new System.Drawing.Point();
        point2.X=0;
        point2.Y=0;
        System.Drawing.RectangleF rec =new System.Drawing.RectangleF(point,size);
        
        //draw an ellipse inscribed in the invisible rectangle rec
        //to change the size or shape of the ellipse change an invisible rectangle in which it is inscribed
        //to change the color of the ellipse, change the color of the pen p which is used to draw it
        
        g.DrawEllipse(p,rec);
    
        //draw a line between a pair of points point1 and point2 with pen p1
        g.DrawLine(p1,point1,point2);
    }
    public static void Main() {
        System.Windows.Forms.Application.Run(new TestGDI1());//display form
    }
}



           
          


11-Pixel Inset Pen

image_pdfimage_print


   

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)
    {
      Rectangle rect = new Rectangle(10, 10, 110, 110);
      Pen pen = new Pen(Color.White, 11);
      Pen penOutline = new Pen(Color.Black, 1);
      penOutline.Alignment = PenAlignment.Inset;
      pen.Alignment = PenAlignment.Center;
      rect.Offset(10, -25);
      e.Graphics.DrawString("11-Pixel Inset Pen", SystemFonts.DefaultFont, Brushes.Black, rect.Location);
      rect.Offset(0, 25);
      pen.Alignment = PenAlignment.Inset;
      e.Graphics.FillRectangle(Brushes.LightBlue, rect);
      e.Graphics.DrawRectangle(pen, rect);
      e.Graphics.DrawRectangle(penOutline, rect);
      
      

      
      pen.Dispose();
    }


    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());
      }

}



           
          


11-Pixel Centered Pen

image_pdfimage_print


   


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)
    {
      Rectangle rect = new Rectangle(10, 10, 110, 110);
      Pen pen = new Pen(Color.White, 11);
      Pen penOutline = new Pen(Color.Black, 1);
      penOutline.Alignment = PenAlignment.Inset;
      pen.Alignment = PenAlignment.Center;
      e.Graphics.DrawString("11-Pixel Centered Pen", SystemFonts.DefaultFont, Brushes.Black, rect.Location);
      rect.Offset(0, 25);
      e.Graphics.FillRectangle(Brushes.LightBlue, rect);
      e.Graphics.DrawRectangle(pen, rect);
      e.Graphics.DrawRectangle(penOutline, rect);
      
      pen.Dispose();
    }


    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());
      }

}


           
          


Pen Dash Caps: Flat, Round, Triangle

image_pdfimage_print
   
 


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class PenDashCaps: Form
{
     MenuItem miChecked;
   
     public static void Main()
     {
          Application.Run(new PenDashCaps());
     }
     public PenDashCaps()
     {
          ResizeRedraw = true; 
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&Width");
   
          int[] aiWidth = { 1, 2, 5, 10, 15, 20, 25 };
   
          foreach (int iWidth in aiWidth)
               Menu.MenuItems[0].MenuItems.Add(iWidth.ToString(), 
                                        new EventHandler(MenuWidthOnClick));
   
          miChecked = Menu.MenuItems[0].MenuItems[0];
          miChecked.Checked = true;
     }
     void MenuWidthOnClick(object obj, EventArgs ea)
     {
          miChecked.Checked = false;
          miChecked = (MenuItem) obj;
          miChecked.Checked = true;
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }      
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Pen pen = new Pen(clr, Convert.ToInt32(miChecked.Text));
          pen.DashStyle = DashStyle.DashDotDot;
   
          foreach (DashCap dc in Enum.GetValues(typeof(DashCap)))
          {
               pen.DashCap = dc;
   
               grfx.DrawLine(pen, cx / 8, cy / 4, 7 * cx / 8, cy / 4); 
               grfx.TranslateTransform(0, cy / 4);
          }
     }
}

    


DashDot style Pen

image_pdfimage_print
   
 
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) {
        Pen p = new Pen(Color.HotPink, 10);
        p.DashStyle = DashStyle.DashDot;
        Graphics g = this.CreateGraphics();
        g.DrawEllipse(p, 10, 15, 105, 250);
    }
}

    


new Pen(ForeColor)

image_pdfimage_print
   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class XMarksTheSpot: Form
{
     public static void Main()
     {
          Application.Run(new XMarksTheSpot());
     }
     public XMarksTheSpot()
     {
          Text = "X Marks The Spot";
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
          Pen      pen  = new Pen(ForeColor);
   
          grfx.DrawLine(pen, 0, 0, 
                             ClientSize.Width - 1, ClientSize.Height - 1);
          grfx.DrawLine(pen, 0, ClientSize.Height - 1, 
                             ClientSize.Width - 1, 0);
     }
}