当前位置: 开源爱好者 » 编程教程 » C#教程 » 正文

C# 获取并显示windows所有字体

本例演示了Visual C# 获取并显示windows所有字体的方法及完整代码,编写时要用到了以下几个类:

InstalledFontCollection
FontFamily
Font

下面是代码:

using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
namespace DisplayFonts
{
    public class DisplayFonts : System.Windows.Forms.Form
    {
        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Button b_DisplayFonts;
        private System.ComponentModel.Container components = null;
        public DisplayFonts()
        {
            InitializeComponent();
        }
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
            #region Windows Form Designer generated code
        private void InitializeComponent()
        {
            this.b_DisplayFonts = new System.Windows.Forms.Button();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            this.b_DisplayFonts.Location = new System.Drawing.Point(154, 268);
            this.b_DisplayFonts.Name = "b_DisplayFonts";
            this.b_DisplayFonts.Size = new System.Drawing.Size(163, 28);
            this.b_DisplayFonts.TabIndex = 2;
            this.b_DisplayFonts.Text = "显示系统所有字体";
            this.b_DisplayFonts.Click += new System.EventHandler(this.DisplayFonts_Click);
            // richTextBox1
            //
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(471, 259);
            this.richTextBox1.TabIndex = 0;
            this.richTextBox1.Text = "";
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(471, 301);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                this.b_DisplayFonts,
                this.richTextBox1});
            this.Name = "DisplayFonts";
            this.Text = "显示字体";
            this.ResumeLayout(false);
        }
           #endregion
        [STAThread]
        static void Main ()
        {
            Application.Run(new DisplayFonts());
        }
        //
        // 显示所有字体
        //
        private void DisplayFonts_Click(object sender, System.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);
                // 注意:每次AppendText之前都设置字体
                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);
        }
    }
}

 实例化InstalledFontCollection类,用它的Families属性得到所有的字体信息,把所有的字体信息存放在FontFamily[]数组中:

FontFamily[] ffs = ifc.Families;

再用foreach循环把所有的字体信息写到richtextbox中,注意,每次写的时候都选了字体。

CopyRight 2018© 开源爱好者 All Rights Reserved 豫ICP备16030607号 隐私声明 网站地图