Font Famylies
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Text; // InstalledFontCollection
namespace FontFamylies
{
/// <summary>
/// Summary description for FontFamylies.
/// </summary>
public class FontFamylies : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
FontFamily[] iFCF;
ArrayList iFCFN;
public FontFamylies()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.Text = "Installed Font Families";
iFCF = null; // fontfamilies
iFCFN = new ArrayList(); // strings
iFCF = InstalledFontFamilies(iFCFN);
this.comboBox1.Sorted = true;
this.comboBox1.DataSource = iFCFN; //set the combo's data source
//
// 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.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "comboBox1";
//
// FontFamylies
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.comboBox1});
this.Name = "FontFamylies";
this.Text = "FontFamylies";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FontFamylies());
}
private FontFamily[] InstalledFontFamilies(ArrayList iFCFN)
{
InstalledFontCollection iFC = new InstalledFontCollection();
foreach(FontFamily ff in iFC.Families)
iFCFN.Add(ff.Name);
return iFC.Families;
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics g = pea.Graphics;
int wi = 150, hi = 12, rectNb = 4;
this.Width = (wi + 2)*rectNb + 9;
int iFCFNb = iFCF.Length;
DisplayInstalledFontFamilies(g, iFCFNb, wi, hi, rectNb);
g.Dispose();
}
private void DisplayInstalledFontFamilies(Graphics g, int iFCFNb, int wi,
int hi, int rectNb)
{
Rectangle rec;
Pen p = new Pen(this.ForeColor);
Brush b = null;
Font f;
StringFormat strfmt = new StringFormat();
strfmt.LineAlignment = strfmt.Alignment = StringAlignment.Near;
int x, y;
for (int i = 0; i < iFCFNb; i++)
{
x = (int)(i % rectNb);
y = (int)(i / rectNb);
rec = new Rectangle(1 + x*(2 + wi), 25 + y*(2 + hi), wi, hi);
g.DrawRectangle(p, rec);
try
{
f = new Font(iFCF[i], 8, FontStyle.Regular, GraphicsUnit.Point);
}
catch
{
// some fonts do not support Regular style
f = new Font("Arial", 8, FontStyle.Strikeout, GraphicsUnit.Point);
}
b = new SolidBrush(Color.Black);
g.DrawString((string)iFCFN[i], f, b, rec, strfmt);
}
p.Dispose(); b.Dispose();
}
}
}
Related examples in the same category