日期:2014-05-19  浏览次数:20802 次

列出所有字体的问题
我用下面代码把系统所装的字体显示在下拉框中.但与vs中工具-> 选项-> 字体和颜色项里字体比较时,发现下拉框里列举的并不全,比如Fixedsys这个字体,下拉框里就没有.请问应该怎么样做才能像VS中全部都列举出来?

  foreach(FontFamily   f   in   FontFamily.Families)
                        cbFont1.Items.Add(f.Name);


------解决方案--------------------
这是我以前写过的代码 你参考一下
C# ComboBox自定义字体和字号列表
磅和字号的关系:
磅 字号
5 八号
5.5 七号
6.5 小六号
7.5 六号
9 小五号
10.5 五号
12 小四号
14 四号
15 小三号
16 三号
18 小二号
22 二号
24 小一号
26 一号
36 小初号
42 初号



////////////////////////////////////
//FontType为ToolStripComboBox
//FontSize为ToolStripComboBox
//richTextBox为RichTextBox
///////////////////////////////////
//
//字号的名字
//
public string[] allFontSizeName ={ "8 ", "9 ", "10 ", "12 ", "14 ", "16 ", "18 ", "20 ", "22 ", "24 ", "26 ", "28 ", "36 ", "48 ", "72 ", "初号 ", "小初 ", "一号 ", "小一 ", "二号 ", "小二 ", "三号 ", "小三 ", "四号 ", "小四 ", "五号 ", "小五 ", "六号 ", "小六 ", "七号 ", "八号 " };
//
//字号的磅值
//
public float[] allFontSize ={ 8,9,10,12,14,16,18,20,22,24,26,28,36,48,72,42,36,26,24,22,18,16,15,14,12,10.5F,9,7.5F,6.5F,5.5F,5};
//
//遍历系统字体并且添加到ToolStripComboBox的列表框里
//设置初始字体为宋体
//
foreach (FontFamily font in FontFamily.Families)
{
this.FontType.Items.Add(font.Name);
}
this.FontType.SelectedItem = "宋体 ";
//
//添加字号
//设置初始字号为小五
//
foreach (string fontSize in allFontSizeName)
{
this.FontSize.Items.Add(fontSize);
}
this.FontSize.SelectedItem = "小五 ";
#region 事件
/// <summary>
/// 应用新字体
/// </summary>
private void FontType_SelectedIndexChanged(object sender, EventArgs e)
{
this.richTextBox.SelectionFont = new Font(this.FontType.Text, this.richTextBox.SelectionFont.Size, this.richTextBox.SelectionFont.Style);
}
/// <summary>
/// 应用新字体字号
/// </summary>
private void FontSize_SelectedIndexChanged(object sender, EventArgs e)
{
this.richTextBox.SelectionFont = new Font(this.richTextBox.SelectionFont.FontFamily, allFontSize[this.FontSize.SelectedIndex], this.richTextBox.SelectionFont.Style);
}
#endregion
------解决方案--------------------
.NET并不是支持所有字体, 好像只支持TTF, OTF CON等好像不支持
------解决方案--------------------
可以考虑使用Windows API。其中EnumFonts和EnumFontFamilies两个函数会比较有帮助。具体可以在msdn中查询Enumerating the Installed Fonts。
另外.Net2003 Option菜单里面font菜单的东西也是不完全的。比如我安装了微软雅黑和微软雅黑粗体,结果就只有粗体,没有雅黑。但是在2005里面就是正常的。所以嘛,不要完全相信它。呵呵。