String Format Flag: Direction Vertical


   



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

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

      String s = "Accrington Stanley";
      StringFormat sf = new StringFormat(StringFormatFlags.DirectionVertical);
      Font f = new Font("Times New Roman", 14);

      SizeF sizef = g.MeasureString(s, f, Int32.MaxValue, sf);

      RectangleF rf = new RectangleF(20, 20, sizef.Width, sizef.Height);
      g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height);
      g.DrawString(s, f, Brushes.Black, rf, sf);

      f.Dispose();
    }

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

           
          


String format flag: No Wrap


   


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

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

      String s = "This string is long enough to wrap. ";

      Font f = new Font("Arial", 12);
      Rectangle r = new Rectangle(20, 20, 150, f.Height * 4);

      StringFormat sf = new StringFormat();
      sf.FormatFlags = StringFormatFlags.NoWrap;

      g.DrawRectangle(Pens.Black, r);
      g.DrawString(s, f, Brushes.Black, r, sf);
      f.Dispose();
    }

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


           
          


Text wrap: vertical, horizontal and trim


   


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
{
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.ComboBox lstAlignmentH;
    private System.Windows.Forms.ComboBox lstTrimming;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.ComboBox lstAlignmentV;
        public Form1() {
            InitializeComponent();
      ResizeRedraw = true;
      lstAlignmentH.DataSource = Enum.GetNames(typeof(StringAlignment));
      lstAlignmentV.DataSource = Enum.GetNames(typeof(StringAlignment));
      lstTrimming.DataSource = Enum.GetNames(typeof(StringTrimming));
        }

    private void TextWrap_Paint(object sender, PaintEventArgs e)
    {
      string text = "Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Center each line of text.Center each line of textCenter each line of textCenter each line of textCenter each line of textCenter each line of text";
          
      StringFormat stringFormat = new StringFormat();
      stringFormat.Alignment = (StringAlignment)Enum.Parse(typeof(StringAlignment), lstAlignmentH.Text);
      stringFormat.LineAlignment = (StringAlignment)Enum.Parse(typeof(StringAlignment), lstAlignmentV.Text);
      
      stringFormat.Trimming = (StringTrimming)Enum.Parse(typeof(StringTrimming), lstTrimming.Text);

      Font font = new Font("Verdana", 12);
      Rectangle rect = new Rectangle(30, 110, Width - 60, Height - 160);
      e.Graphics.DrawString(text, font, Brushes.Black, rect, stringFormat);

      Pen pen = Pens.Black;
      e.Graphics.DrawRectangle(pen, rect);
    }

    private void lst_Changed(object sender, EventArgs e)
    {
      Invalidate();
    }
    private void InitializeComponent()
    {
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.lstAlignmentH = new System.Windows.Forms.ComboBox();
      this.lstTrimming = new System.Windows.Forms.ComboBox();
      this.label3 = new System.Windows.Forms.Label();
      this.lstAlignmentV = new System.Windows.Forms.ComboBox();
      this.SuspendLayout();
      // 
      // label1
      // 
      this.label1.AutoSize = true;
      this.label1.Location = new System.Drawing.Point(12, 19);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(113, 13);
      this.label1.TabIndex = 0;
      this.label1.Text = "Alignment (Horizontal):";
      // 
      // label2
      // 
      this.label2.AutoSize = true;
      this.label2.Location = new System.Drawing.Point(12, 74);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(49, 13);
      this.label2.TabIndex = 1;
      this.label2.Text = "Trimming:";
      // 
      // lstAlignmentH
      // 
      this.lstAlignmentH.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.lstAlignmentH.FormattingEnabled = true;
      this.lstAlignmentH.Location = new System.Drawing.Point(131, 16);
      this.lstAlignmentH.Name = "lstAlignmentH";
      this.lstAlignmentH.Size = new System.Drawing.Size(121, 21);
      this.lstAlignmentH.TabIndex = 2;
      this.lstAlignmentH.SelectedIndexChanged += new System.EventHandler(this.lst_Changed);
      // 
      // lstTrimming
      // 
      this.lstTrimming.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.lstTrimming.FormattingEnabled = true;
      this.lstTrimming.Location = new System.Drawing.Point(131, 69);
      this.lstTrimming.Name = "lstTrimming";
      this.lstTrimming.Size = new System.Drawing.Size(121, 21);
      this.lstTrimming.TabIndex = 3;
      this.lstTrimming.SelectedIndexChanged += new System.EventHandler(this.lst_Changed);
      // 
      // label3
      // 
      this.label3.AutoSize = true;
      this.label3.Location = new System.Drawing.Point(12, 47);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(100, 13);
      this.label3.TabIndex = 4;
      this.label3.Text = "Alignment (Vertical):";
      // 
      // lstAlignmentV
      // 
      this.lstAlignmentV.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.lstAlignmentV.FormattingEnabled = true;
      this.lstAlignmentV.Location = new System.Drawing.Point(131, 42);
      this.lstAlignmentV.Name = "lstAlignmentV";
      this.lstAlignmentV.Size = new System.Drawing.Size(121, 21);
      this.lstAlignmentV.TabIndex = 5;
      this.lstAlignmentV.SelectedIndexChanged += new System.EventHandler(this.lst_Changed);
      // 
      // TextWrap
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(319, 296);
      this.Controls.Add(this.lstAlignmentV);
      this.Controls.Add(this.label3);
      this.Controls.Add(this.lstTrimming);
      this.Controls.Add(this.lstAlignmentH);
      this.Controls.Add(this.label2);
      this.Controls.Add(this.label1);
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.Name = "TextWrap";
      this.Text = "Text Wrap";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.TextWrap_Paint);
      this.ResumeLayout(false);
      this.PerformLayout();

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

}


           
          


Text Columns

   
 


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
   
class TextColumns: Form
{
     public static void Main()
     {
          Application.Run(new TextColumns());
     }
     public TextColumns()
     {
          Font = new Font("Times New Roman", 10);
          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);
          int          iChars, iLines;
          string       str    = "Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text ";
          StringFormat strfmt = new StringFormat();
   
  
          PointF[] aptf = { new PointF(cx, cy) };
          grfx.TransformPoints(CoordinateSpace.Device, 
                               CoordinateSpace.Page, aptf);
   
          grfx.PageUnit  = GraphicsUnit.Point;
   
          grfx.TransformPoints(CoordinateSpace.Page, 
                               CoordinateSpace.Device, aptf);
          float fcx = aptf[0].X;
          float fcy = aptf[0].Y;
   
          strfmt.HotkeyPrefix = HotkeyPrefix.Show;
          strfmt.Trimming     = StringTrimming.Word;
          strfmt.FormatFlags |= StringFormatFlags.NoClip; 
          strfmt.SetTabStops(0, new float[] { 18 });
   
          for (int x = 0; x < fcx &amp;&amp; str.Length > 0; x += 156)
          {
               RectangleF rectf = new RectangleF(x, 0, 144, 
                                                 fcy - Font.GetHeight(grfx));
   
               grfx.DrawString(str, Font, brush, rectf, strfmt);
               grfx.MeasureString(str, Font, rectf.Size, strfmt, 
                                  out iChars, out iLines);
   
               str = str.Substring(iChars);
          }
   
     }
}

    


new SolidBrush(ForeColor)

   
 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class HelloCenteredMeasured: Form
{
     public static void Main() 
     {
          Application.Run(new HelloCenteredMeasured()); 
     }
     public HelloCenteredMeasured()
     {
          Text = "Hello Centered Using MeasureString";
          BackColor = SystemColors.Window;
          ForeColor = SystemColors.WindowText;
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics graphics      = pea.Graphics;
          string   str       = "Hello, world!";
          SizeF    sizefText = graphics.MeasureString(str, Font);
   
          graphics.DrawString(str, Font, new SolidBrush(ForeColor), 
                          (ClientSize.Width  - sizefText.Width)  / 2, 
                          (ClientSize.Height - sizefText.Height) / 2);
     }
}

    


Solid Brush: Firebrick


   
 

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

public class MainForm : Form {
    public static void Main() {
        Application.Run(new MainForm());
    }

    protected void OnPaint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;
        SolidBrush blueBrush = new SolidBrush(Color.Blue);
        SolidBrush pen2 = (SolidBrush)Brushes.Firebrick;
        g.FillEllipse(blueBrush, 10, 10, 100, 100);
        g.FillPie(Brushes.Black, 150, 10, 120, 150, 90, 80);
        SolidBrush brush3 = new SolidBrush(Color.Purple);
        g.FillPolygon(brush3,
            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)});
        Rectangle r = new Rectangle(150, 10, 130, 60);
        g.FillRectangle(Brushes.Blue, r);
        g.DrawString("Hello out there...How are ya?", new Font("Arial", 12), Brushes.White, r);
    }
}

    


SmoothingMode: HighQuality


   


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

public class Form1 : Form
{
    private System.Windows.Forms.PictureBox picNone;

      public Form1() {
            InitializeComponent();
      }
    private void picNone_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
        
      Pen drawingPen = new Pen(Color.Red, 5);
      e.Graphics.DrawEllipse(drawingPen, 10, 10, 300, 40);
    }

    private void InitializeComponent()
    {
      this.picNone = new System.Windows.Forms.PictureBox();
      this.SuspendLayout();
      // 
      // picNone
      // 
      this.picNone.Location = new System.Drawing.Point(8, 16);
      this.picNone.Name = "picNone";
      this.picNone.Size = new System.Drawing.Size(328, 64);
      this.picNone.TabIndex = 0;
      this.picNone.TabStop = false;
      this.picNone.Paint += new System.Windows.Forms.PaintEventHandler(this.picNone_Paint);
      // 
      // Smoothing
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(392, 382);
      this.Controls.Add(this.picNone);
      this.Name = "Smoothing";
      this.Text = "Smoothing";
      this.ResumeLayout(false);

    }

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

}