Display System Fonts




Description

This sample shows how to display all the fonts on a Windows system.

I have used these classes:

  • InstalledFontCollection
  • FontFamily
  • Font
  • richTextBox

Initializing InstalledFontCollection and then using the .Families property allows me to return the names of all the font familys as an array of FontFamily like this

FontFamily[] ffs = ifc.Families;

I can then use a simple foreach loop to loop through all the font families and create fonts for each family and then write a line of text to the richtextbox.

Note that I have to select the font for the richtextbox control every time I write or append a line of text to it.

Source Code:

// DisplayFonts
// Lists all the fonts on a windows system
// Written by J O'Donnell ([email protected]) , 19/09/01

using
System;
using
System.Drawing;
using
System.Drawing.Text;
using
System.Windows.Forms;
namespace
DisplayFonts
{
///
<summary>
///
Summary description for Form1.
/// </summary>

public class
DisplayFonts : System.Windows.Forms.Form
{
private
System.Windows.Forms.RichTextBox richTextBox1;
private
System.Windows.Forms.Button b_DisplayFonts;
///
<summary>
///
Required designer variable.
///
</summary>
private System.ComponentModel.Container components = null
;
public
DisplayFonts()
{
//
// 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.richTextBox1 = new
System.Windows.Forms.RichTextBox();
this.b_DisplayFonts = new
System.Windows.Forms.Button();
this
.SuspendLayout();
//
// richTextBox1
//
this
.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new
System.Drawing.Size(368, 224);
this
.richTextBox1.TabIndex = 0;
this
.richTextBox1.Text = "";
//
// b_DisplayFonts
//
this.b_DisplayFonts.Location = new
System.Drawing.Point(120, 232);
this
.b_DisplayFonts.Name = "b_DisplayFonts";
this.b_DisplayFonts.Size = new
System.Drawing.Size(128, 24);
this
.b_DisplayFonts.TabIndex = 2;
this
.b_DisplayFonts.Text = "DisplayFonts";
this
.b_DisplayFonts.Click += new System.EventHandler(this.DisplayFonts_Click); 
//
// DisplayFonts
//
this.AutoScaleBaseSize = new
System.Drawing.Size(5, 13);
this.ClientSize = new
System.Drawing.Size(368, 261);
this.Controls.AddRange(new
System.Windows.Forms.Control[] {this.b_DisplayFonts, this.richTextBox1});
this
.Name = "DisplayFonts";
this
.Text = "DisplayFonts";
this.ResumeLayout(false
);
}
#endregion

///
<summary>
///
The main entry point for the application.
/// </summary>

[STAThread]
static void Main ()
{
Application.Run(
new
DisplayFonts());
}
private void DisplayFonts_Click(object
sender, System.EventArgs e)
{
InstalledFontCollection ifc =
new
InstalledFontCollection();
FontFamily[] ffs = ifc.Families;
Font f;
richTextBox1.Clear();
foreach(FontFamily ff in
ffs)
{
if
(ff.IsStyleAvailable(System.Drawing.FontStyle.Regular))
f =
new
Font(ff.GetName(1),12,System.Drawing.FontStyle.Regular);
else if
(ff.IsStyleAvailable(System.Drawing.FontStyle.Bold))
f =
new
Font(ff.GetName(1),12, System.Drawing.FontStyle.Bold);
else if
(ff.IsStyleAvailable(System.Drawing.FontStyle.Italic))
f =
new
Font(ff.GetName(1),12, System.Drawing.FontStyle.Italic);
else
f = new
Font(ff.GetName(1),12, System.Drawing.FontStyle.Underline);
richTextBox1.SelectionFont=f;
richTextBox1.AppendText(ff.GetName(1)+"\r\n");richTextBox1.SelectionFont=f;
richTextBox1.AppendText("abcdefghijklmnopqrstuvwxyz\r\n");
richTextBox1.SelectionFont=f;
richTextBox1.AppendText("ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n");
richTextBox1.AppendText"==========================\r\n");
}
MessageBox.Show("finished adding fonts to window","DisplayFonts");
}
}
}


Similar Articles