How can I draw font samples from the available fonts

Platform: WinForms|Category: Fonts

James DeBroeck of Microsoft gave this response on the microsoft.public.dotnet.framework.windowsforms.

Here is some C# OnPaint code that will show you how to use FontFamily to get a list of fonts:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
    int i, nCount;
    SolidBrush  b = new SolidBrush( Color.Black );
    int x = 0, y = 0;

    nCount = FontFamily.Families.Length;
    
    for(i=0;i<nCount;i++)
    {
        FontStyle fs = FontStyle.Regular;
        Font f;
        string StylesDesc = '';

        if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Regular))
            StylesDesc += 'Regular ';
        else if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Italic))
        {
            fs = FontStyle.Italic;
            StylesDesc += 'Italic ';
        }
        else if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Bold))
        {
            fs = FontStyle.Bold;
            StylesDesc += 'Bold ';
        }
        else if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Underline))
        {
            fs = FontStyle.Underline;
            StylesDesc += 'Underline ';
        }
        else if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Strikeout))
        {
            fs = FontStyle.Strikeout;
            StylesDesc += 'Strikeout ';
        }

        if (StylesDesc.Length > 0)
	    StylesDesc = StylesDesc.Substring(0,StylesDesc.Length-1);

        f = new Font( FontFamily.Families[i].Name, 12, fs);

        String s = FontFamily.Families[i].Name + ' ' + StylesDesc;
        e.Graphics.DrawString( s, f, b, x, y );
        y += f.Height;

    }
}