using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Text; using System.Text; using System.Windows.Forms; public class Form1 : Form { public Form1() { this.BackColor = Color.White; } protected override void OnPaint(PaintEventArgs e) { int verticalCoordinate = 10; Point topLeftCorner; InstalledFontCollection insFont = new InstalledFontCollection(); FontFamily[] families = insFont.Families; e.Graphics.TranslateTransform(AutoScrollPosition.X,AutoScrollPosition.Y); foreach (FontFamily family in families) { if (family.IsStyleAvailable(FontStyle.Regular)) { Font f = new Font(family.Name, 10); topLeftCorner = new Point(10, verticalCoordinate); verticalCoordinate += f.Height; e.Graphics.DrawString(family.Name, f, Brushes.Black, topLeftCorner); f.Dispose(); } } } public static void Main() { Application.Run(new Form1()); } }
FontFamilies: IsStyleAvailable,
using System; using System.Drawing; using System.Drawing.Text; using System.Drawing.Drawing2D; using System.Windows.Forms; class FamiliesList : Form { const int iPointSize = 12; public static void Main() { Application.Run(new FamiliesList()); } public FamiliesList() { Text = "Font Families List"; 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 x = 0, y = 0, fMaxWidth = 0; FontCollection fc = new InstalledFontCollection(); FontFamily[] aff = fc.Families; foreach (FontFamily ff in aff) { Font font = CreateSampleFont(ff, iPointSize); SizeF sizef = grfx.MeasureString(ff.Name, font); fMaxWidth = Math.Max(fMaxWidth, sizef.Width); } foreach (FontFamily ff in aff) { Font font = CreateSampleFont(ff, iPointSize); float fHeight = font.GetHeight(grfx); if (y > 0 && y + fHeight > cy) { x += fMaxWidth; y = 0; } grfx.DrawString(ff.Name, font, brush, x, y); y += fHeight; } } Font CreateSampleFont(FontFamily ff, float fPointSize) { if (ff.IsStyleAvailable(FontStyle.Regular)) return new Font(ff, fPointSize); else if (ff.IsStyleAvailable(FontStyle.Bold)) return new Font(ff, fPointSize, FontStyle.Bold); else if (ff.IsStyleAvailable(FontStyle.Italic)) return new Font(ff, fPointSize, FontStyle.Italic); else return Font; } }
Write font metrics for Times New Roman font family
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class MainClass { public static void Main() { String formatString = "{0,-16}{1,8}{2,9}{3,10}{4,14}"; Console.WriteLine(formatString, "Font Family Name", "Ascent", "Descent","EmHeight", "Line Spacing"); FontFamily ff = new FontFamily("Times New Roman"); Console.WriteLine(formatString, ff.GetName(0), ff.GetCellAscent(FontStyle.Regular), ff.GetCellDescent(FontStyle.Regular), ff.GetEmHeight(FontStyle.Regular), ff.GetLineSpacing(FontStyle.Regular)); } }
Write font metrics for Courier New font family
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class MainClass { public static void Main() { String formatString = "{0,-16}{1,8}{2,9}{3,10}{4,14}"; Console.WriteLine(formatString, "Font Family Name", "Ascent", "Descent","EmHeight", "Line Spacing"); // Write font metrics for Courier New font family FontFamily ff = new FontFamily("Courier New"); Console.WriteLine(formatString, ff.GetName(0), ff.GetCellAscent(FontStyle.Regular), ff.GetCellDescent(FontStyle.Regular), ff.GetEmHeight(FontStyle.Regular), ff.GetLineSpacing(FontStyle.Regular)); } }
Write font metrics for Arial font family
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class MainClass { public static void Main() { String formatString = "{0,-16}{1,8}{2,9}{3,10}{4,14}"; Console.WriteLine(formatString, "Font Family Name", "Ascent", "Descent","EmHeight", "Line Spacing"); FontFamily ff = new FontFamily("Arial"); Console.WriteLine(formatString, ff.GetName(0), ff.GetCellAscent(FontStyle.Regular), ff.GetCellDescent(FontStyle.Regular), ff.GetEmHeight(FontStyle.Regular), ff.GetLineSpacing(FontStyle.Regular)); } }
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
{
///
///
public class FontFamylies : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
///
///
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
//
}
///
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// the contents of this method with the code editor.
///
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
///
///
[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();
}
}
}
[/csharp]
Smoothing Fonts
/* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Drawing.Text; namespace GDI_Basics { /// <summary> /// Summary description for SmoothingFonts. /// </summary> public class SmoothingFonts : System.Windows.Forms.Form { internal System.Windows.Forms.Label Label3; internal System.Windows.Forms.Label Label2; internal System.Windows.Forms.Label Label1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public SmoothingFonts() { // // 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.Label3 = new System.Windows.Forms.Label(); this.Label2 = new System.Windows.Forms.Label(); this.Label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // Label3 // this.Label3.BackColor = System.Drawing.Color.White; this.Label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.Label3.Location = new System.Drawing.Point(272, 168); this.Label3.Name = "Label3"; this.Label3.Size = new System.Drawing.Size(128, 16); this.Label3.TabIndex = 5; this.Label3.Text = " ClearTypeGridFit"; // // Label2 // this.Label2.BackColor = System.Drawing.Color.White; this.Label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.Label2.Location = new System.Drawing.Point(272, 100); this.Label2.Name = "Label2"; this.Label2.Size = new System.Drawing.Size(128, 16); this.Label2.TabIndex = 4; this.Label2.Text = " AntiAliasGridFit"; // // Label1 // this.Label1.BackColor = System.Drawing.Color.White; this.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.Label1.Location = new System.Drawing.Point(272, 36); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(128, 16); this.Label1.TabIndex = 3; this.Label1.Text = " SingleBitPerPixelGridFit"; // // SmoothingFonts // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(440, 314); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.Label3, this.Label2, this.Label1}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "SmoothingFonts"; this.Text = "SmoothingFonts"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.SmoothingFonts_Paint); this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new SmoothingFonts()); } private void SmoothingFonts_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Font TextFont = new Font("Times New Roman", 25, FontStyle.Italic); e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; e.Graphics.DrawString("Sample Text", TextFont, Brushes.Black, 20, 20); e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; e.Graphics.DrawString("Sample Text", TextFont, Brushes.Black, 20, 85); e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; e.Graphics.DrawString("Sample Text", TextFont, Brushes.Black, 20, 150); } } }