10.12 Enumerating System Fonts

 <  Day Day Up  >  

You want to retrieve a list of the fonts installed on a user 's system.


Technique

You can get the list of fonts on a user's system by using an InstalledFontCollection object. After creating this object, enumerate the Families array to access each FontFamily object. The name of the font is contained within the GetName method:

 
 public Form1() {     //     // Required for Windows Form Designer support     //     InitializeComponent();     // enumerate system fonts and add to combobox     InstalledFontCollection installedFonts = new InstalledFontCollection();     foreach(FontFamily family in installedFonts.Families)     {         cbFonts.Items.Add( family.GetName(0) );     }     cbFonts.SelectedIndex = 0; } 

Comments

If your application is document centric, meaning the user manipulates a document containing some sort of textual information, you should allow the user to change the font to better suit her preferences. Due to the rich collections support built into the .NET Framework, enumerating the list of installed fonts on a user's system is the same as enumerating any list-based collection that you've seen so far. You use the InstalledFontCollection to enumerate each FontFamily object in the collection. Each FontFamily object contains a method named GetName that you use to retrieve the name associated with that family.

You might be wondering why the term family refers to fonts. A font family is a group of fonts that share the same basic rendering characteristics. Let's use Arial as an example. Arial by itself is considered a font family. Once you start adding characteristics, such as a bold style or character size , you then have a font. When you create a new Font object, the first parameter to the constructor is the font family, followed by any additional characteristics needed for the final font.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net