11-Pixel Centered Pen


   


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

   
 


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

   
 
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)

   
 


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

    


new Pen(clr, f)

using System;
using System.Drawing;
using System.Windows.Forms;

class PenWidths: Form
{
public static void Main()
{
Application.Run(new PenWidths());
}
public PenWidths()
{
Text = “Pen Widths”;
ResizeRedraw = true;
}
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)
{
Brush brush = new SolidBrush(clr);
float y = 0;

grfx.PageUnit = GraphicsUnit.Point;
grfx.PageScale = 1;

for (float f = 0; f < 3.2; f += 0.2f) { Pen pen = new Pen(clr, f); string str = String.Format("{0:F1} point wide pen: ", f); SizeF sizef = grfx.MeasureString(str, Font); grfx.DrawString(str, Font, brush, 0, y); grfx.DrawLine(pen, sizef.Width, y + sizef.Height / 2, sizef.Width + 144, y + sizef.Height / 2); y += sizef.Height; } } } [/csharp]

new Pen(lgbrush, Math.Min(cx, cy) / 25), Gradient Pen

   
 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class GradientPen: Form 
{
     public static void Main()
     {
          Application.Run(new GradientPen());
     }
     public GradientPen()
     {
          ResizeRedraw = true; 
     }
     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)
     {
          Brush lgbrush = new LinearGradientBrush(
                                   new Rectangle(0, 0, cx, cy), 
                                   Color.White, Color.Black,
                                   LinearGradientMode.BackwardDiagonal);
   
          Pen pen = new Pen(lgbrush, Math.Min(cx, cy) / 25);
   
          pen.Alignment = PenAlignment.Inset;
   
          grfx.DrawRectangle(pen, 0, 0, cx, cy);
          grfx.DrawLine(pen, 0, 0, cx, cy);
          grfx.DrawLine(pen, 0, cy, cx, 0);
     }
}

    


Two-Triangle Tile

   
 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class TwoTriangleTile : Form {
    const int iSide = 50;         // Side of square for triangle

    public static void Main() {
        Application.Run(new TwoTriangleTile());
    }
    public TwoTriangleTile() {
        ResizeRedraw = true;
    }
    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) {
        Point[] apt = { new Point(0, 0), new Point(iSide, 0), new Point(0, iSide) };

        PathGradientBrush pgbrush1 =
                       new PathGradientBrush(apt, WrapMode.TileFlipXY);

        apt = new Point[] {new Point(iSide, 0), new Point(iSide, iSide), 
                             new Point(0, iSide)};

        PathGradientBrush pgbrush2 =
                       new PathGradientBrush(apt, WrapMode.TileFlipXY);

        grfx.FillRectangle(pgbrush1, 0, 0, cx, cy);
        grfx.FillRectangle(pgbrush2, 0, 0, cx, cy);
    }
}