Font list


   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace FontList
{
  /// <summary>
  /// Summary description for FontListForm.
  /// </summary>
  public class FontListForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox listBox1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public FontListForm()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      InitForm ();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    FontFamily [] fonts;
    protected void InitForm ()
    {
      fonts = FontFamily.Families;
      foreach (FontFamily font in fonts)
        listBox1.Items.Add (font.Name);
    }
    /// <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()
    {
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.SuspendLayout();
      // 
      // listBox1
      // 
      this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
      this.listBox1.Location = new System.Drawing.Point(22, 21);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(226, 190);
      this.listBox1.TabIndex = 0;
      this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_OnDoubleClick);
      this.listBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.listBox1_OnMeasureItem);
      this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_OnDrawItem);
      this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_OnSelChanged);
      // 
      // FontListForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(266, 243);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.listBox1});
      this.Name = "FontListForm";
      this.Text = "Font Families";
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new FontListForm());
    }

    private void listBox1_OnDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
      Rectangle rc = e.Bounds;
      string strSample = "The quick red fox jumps over the lazy brown dog";
      Font fntText;
      Font fntSample;
      int cy = 3 * (rc.Bottom - rc.Top) / 4;
      try
      {
        fntText = new Font ("Times New Roman", 8, GraphicsUnit.Point);
      }
      catch (ArgumentException)
      {
        MessageBox.Show ("Cannot create Times New Roman font", "Pied font",
          MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return;
      }
      try
      {
        fntText = new Font ("Times New Roman", 8, GraphicsUnit.Point);
        fntSample = new Font (fonts[e.Index], 8, GraphicsUnit.Point);
      }
      catch (ArgumentException)
      {
        DialogResult mbResult = MessageBox.Show("Cannot open font " + fonts[e.Index].Name +"
Do you want to delete this item from the list","Pied Font Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        switch (mbResult)
        {
          case DialogResult.Yes:
            listBox1.Items.RemoveAt (e.Index);
            return;
          case DialogResult.No:
            break;
        }
        strSample = "Cannot open font " + fonts[e.Index].Name;
        fntSample = fntText;
      }
      Brush brush;
      string str = fonts[e.Index].Name + " -- ";
      if ((e.State &amp; DrawItemState.Selected) != 0)
      {
        e.Graphics.FillRectangle (Brushes.DarkBlue, rc);
        e.DrawFocusRectangle ();
        brush = Brushes.White;
      }
      else
      {
        e.Graphics.FillRectangle (Brushes.White, rc);
        brush = Brushes.Black;
      }
      e.Graphics.DrawString (str, fntText, brush, rc);
      StringFormat format = new StringFormat ();
      format.LineAlignment = StringAlignment.Center;
      format.FormatFlags = StringFormatFlags.LineLimit;
      SizeF size = e.Graphics.MeasureString (str, fntText);
      rc = new Rectangle (rc.Left + (int) (size.Width + .5),
        rc.Top, rc.Width, rc.Height);
      e.Graphics.DrawString (strSample, fntSample, brush, rc, format);
      fntText.Dispose ();
      fntSample.Dispose();
    }

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

    private void listBox1_OnMeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
    {
      MeasureItemEventArgs args = e;
      Font plain = new Font ("Times New Roman", 10, GraphicsUnit.Point);
      SizeF size = e.Graphics.MeasureString ("Test", plain);
      args.ItemHeight = (int) (size.Height + .5);
      plain.Dispose ();
    }

    private void listBox1_OnDoubleClick(object sender, System.EventArgs e)
    {
      if (e is ListChangedEventArgs)
      {
        frmSampleText stuff = new frmSampleText ();
      }
    }
  }
  /// <summary>
  /// Summary description for frmSampleText.
  /// </summary>
  public class frmSampleText : System.Windows.Forms.Form
  {
    private System.Windows.Forms.TextBox textBox1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public frmSampleText()
    {
      //
      // 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()
    {
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.SuspendLayout();
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(8, 8);
      this.textBox1.Multiline = true;
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(272, 136);
      this.textBox1.TabIndex = 0;
      this.textBox1.Text = "textBox1";
      // 
      // frmSampleText
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
      this.ClientSize = new System.Drawing.Size(296, 157);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.textBox1});
      this.Name = "frmSampleText";
      this.Text = "Sample Text";
      this.ResumeLayout(false);

    }
    #endregion
  }

}


           
          


Get all system installed font

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

public class Test{
static void Main()
{
InstalledFontCollection fonts = new InstalledFontCollection();
for(int i = 0; i < fonts.Families.Length; i++) { Console.WriteLine(fonts.Families[i].Name); } } } [/csharp]

Timer based animation for font


   


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


  public class FontForm : System.Windows.Forms.Form
  {
    private Timer timer;
    private int swellValue;
    private string fontFace = "WingDings";

    public FontForm()
    {
      InitializeComponent();
      timer = new Timer();

      Text = "Font App";
      Width = 425;
      Height = 150;
      BackColor = Color.Honeydew;
      CenterToScreen();
      timer.Enabled = true;
      timer.Interval = 100;
      timer.Tick += new EventHandler(FontForm_OnTimer);
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 253);
      this.Text = "Form1";
      this.Resize += new System.EventHandler(this.FontForm_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.FontForm_Paint);

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

    private void FontForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      Font theFont = new Font(fontFace, 12 + swellValue);

      string message = "www.kutayzorlu.com/java2s/com";  
  
      float windowCenter = this.DisplayRectangle.Width / 2;             
      SizeF stringSize = e.Graphics.MeasureString(message, theFont);
      float startPos = windowCenter - (stringSize.Width / 2);

      g.DrawString(message, theFont, 
        new SolidBrush(Color.Blue), startPos, 10);
    }

    private void FontForm_Resize(object sender, System.EventArgs e)
    {
      Rectangle myRect = new Rectangle(0, 100, 
        ClientRectangle.Width, ClientRectangle.Height); 
      Invalidate(myRect);
    }

    private void FontForm_OnTimer(object sender, EventArgs e)
    {
      swellValue += 5;
      if(swellValue >= 50)
        swellValue = 0;

      Invalidate(new Rectangle(0, 0, ClientRectangle.Width, 100));
    }
  }

           
          


Load all system installed fonts


   


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.GroupBox groupBox1;
  private System.Windows.Forms.ComboBox lstFonts;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.StatusStrip statusBar;
  private System.Windows.Forms.ToolStripStatusLabel statusLabel;

  public Form1() {
        InitializeComponent();
    System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
    foreach (FontFamily family in fonts.Families)
    {
      lstFonts.Items.Add(family.Name);
    }
  }

  private void lstFonts_SelectedIndexChanged(object sender, EventArgs e)
  {
        this.Invalidate();
  }

  private void Form1_Paint(object sender, PaintEventArgs e)
  {
    if (lstFonts.SelectedIndex != -1)
    {
            e.Graphics.DrawString(lstFonts.Text, new Font(lstFonts.Text, 50), Brushes.Black, 10, 50);
            statusBar.Items[0].Text = lstFonts.Text;
    }
  }

  private void InitializeComponent()
  {
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.lstFonts = new System.Windows.Forms.ComboBox();
    this.label1 = new System.Windows.Forms.Label();
    this.statusBar = new System.Windows.Forms.StatusStrip();
    this.statusLabel = new System.Windows.Forms.ToolStripStatusLabel();
    this.groupBox1.SuspendLayout();
    this.statusBar.SuspendLayout();
    this.SuspendLayout();
    // 
    // groupBox1
    // 
    this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
          | System.Windows.Forms.AnchorStyles.Right)));
    this.groupBox1.Controls.Add(this.lstFonts);
    this.groupBox1.Controls.Add(this.label1);
    this.groupBox1.Location = new System.Drawing.Point(7, 0);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.Size = new System.Drawing.Size(497, 40);
    this.groupBox1.TabIndex = 1;
    this.groupBox1.TabStop = false;
    // 
    // lstFonts
    // 
    this.lstFonts.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.lstFonts.DropDownWidth = 340;
    this.lstFonts.FormattingEnabled = true;
    this.lstFonts.Location = new System.Drawing.Point(100, 12);
    this.lstFonts.Name = "lstFonts";
    this.lstFonts.Size = new System.Drawing.Size(340, 21);
    this.lstFonts.TabIndex = 1;
    this.lstFonts.SelectedIndexChanged += new System.EventHandler(this.lstFonts_SelectedIndexChanged);
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(12, 16);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(80, 12);
    this.label1.TabIndex = 0;
    this.label1.Text = "Choose Font:";
    // 
    // statusBar
    // 
    this.statusBar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.statusLabel});
    this.statusBar.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Table;
    this.statusBar.Location = new System.Drawing.Point(0, 155);
    this.statusBar.Name = "statusBar";
    this.statusBar.Size = new System.Drawing.Size(516, 22);
    this.statusBar.TabIndex = 2;
    this.statusBar.Text = "statusStrip1";
    // 
    // statusLabel
    // 
    this.statusLabel.Name = "statusLabel";
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(516, 177);
    this.Controls.Add(this.groupBox1);
    this.Controls.Add(this.statusBar);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Text = "Font Viewer";
    this.groupBox1.ResumeLayout(false);
    this.statusBar.ResumeLayout(false);
    this.ResumeLayout(false);
    this.PerformLayout();

  }

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

}


           
          


System Fonts: Icon Title Font


   


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

public class Form1 : Form
{
    private System.Windows.Forms.Label label2;
    
  public Form1() {
        this.Font = SystemFonts.IconTitleFont;
        this.AutoScaleDimensions = new System.Drawing.SizeF(6.0F, 13.0F);
        InitializeComponent();
        SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
  }

    private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
    {
        if (e.Category == UserPreferenceCategory.Window)
        {
            this.Font = SystemFonts.IconTitleFont;
        }
    }

    private void InitializeComponent()
    {
        this.label2 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // label2
        // 
        this.label2.Location = new System.Drawing.Point(12, 9);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(220, 55);
        this.label2.TabIndex = 2;
        this.label2.Text = "Try changing the Small Fonts/Large Fonts setting for th" +
            "e computer.";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(244, 138);
        this.Controls.Add(this.label2);
        this.Text = "Form1";
        this.ResumeLayout(false);

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

}


           
          


Font: GetHeight

   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class FontMenuForm: Form
{
     protected string strText = "Sample Text";
     protected Font font = new Font("Times New Roman", 24, FontStyle.Italic);
   
     public static void Main()
     {
          Application.Run(new FontMenuForm());
     }
     public FontMenuForm()
     {
          ResizeRedraw = true;
          Menu = new MainMenu();
          Menu.MenuItems.Add("&amp;Font!", new EventHandler(MenuFontOnClick));
     }
     void MenuFontOnClick(object obj, EventArgs ea)
     {
          FontDialog dlg = new FontDialog();
          dlg.Font = font;
   
          if (dlg.ShowDialog() == DialogResult.OK)
          {
               font = dlg.Font;
               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)
     {
          SizeF sizef = grfx.MeasureString(strText, font);
          Brush brush = new SolidBrush(clr);
   
          grfx.DrawString(strText, font, brush, (cx - sizef.Width) / 2,
                                                (cy - sizef.Height) / 2);
     }
     public float GetAscent(Graphics grfx, Font font)
     {
          return font.GetHeight(grfx) * 
                    font.FontFamily.GetCellAscent(font.Style) /
                         font.FontFamily.GetLineSpacing(font.Style);
     }
     public float GetDescent(Graphics grfx, Font font)
     {
          return font.GetHeight(grfx) * 
                    font.FontFamily.GetCellDescent(font.Style) /
                         font.FontFamily.GetLineSpacing(font.Style);
     }
     public float PointsToPageUnits(Graphics grfx, Font font)
     {
          float fFontSize;
   
          if (grfx.PageUnit == GraphicsUnit.Display)
               fFontSize = 100 * font.SizeInPoints / 72;
          else
               fFontSize = grfx.DpiX * font.SizeInPoints / 72;
   
          return fFontSize;
     }
}

    


Font with different size

   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class TwentyFourPointPrinterFonts: Form
{
     public static void Main()
     {
          Application.Run(new TwentyFourPointPrinterFonts());
     }
     public TwentyFourPointPrinterFonts()
     {
          Text = "Twenty-Four Point Printer Fonts";
          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;
          Font   font;
          string strFamily = "Times New Roman";
   
          font = new Font(strFamily, 24);
          grfx.DrawString("No GraphicsUnit, 24 points", font, brush, 0, y);
          y += font.GetHeight(grfx);
   
          font = new Font(strFamily, 24, GraphicsUnit.Point);
          grfx.DrawString("GraphicsUnit.Point, 24 units", font, brush, 0, y);
          y += font.GetHeight(grfx);
   
          font = new Font(strFamily, 1/ 3f, GraphicsUnit.Inch);
          grfx.DrawString("GraphicsUnit.Inch, 1/3 units", font, brush, 0, y);
          y += font.GetHeight(grfx);
   
          font = new Font(strFamily, 25.4f / 3, GraphicsUnit.Millimeter);
          grfx.DrawString("GraphicsUnit.Millimeter, 25.4/3 units", 
                          font, brush, 0, y);
          y += font.GetHeight(grfx);
   
          font = new Font(strFamily, 100, GraphicsUnit.Document);
          grfx.DrawString("GraphicsUnit.Document, 100 units", 
                          font, brush, 0, y);
          y += font.GetHeight(grfx);
   
          font = new Font(strFamily, 100f / 3, GraphicsUnit.Pixel);
          grfx.DrawString("GraphicsUnit.Pixel, " + 100f / 3 + " units",
                          font, brush, 0, y);
          y += font.GetHeight(grfx);
   
          font = new Font(strFamily, 100f / 3, GraphicsUnit.World);
          grfx.DrawString("GraphicsUnit.World, " + 100f / 3 + " units", 
                          font, brush, 0, y);
     }
}