代码改变世界

显示系统所有字体

2012-04-08 16:33  精诚所至 金石为开  阅读(564)  评论(0编辑  收藏  举报

本实例创建一个能显示系统已安装的字体的程序,并将字体信息显示在RichTextBox中,程序运行结果如下图。

20120409163115

程序代码如下。

using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
namespace eg48_displayFont
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}
		
		void B_DisplayFontsClick(object sender, 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("已经把所有字体显示在文本框中","成功",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
		}
	}
}