C#读取系统字体颜色与大小的方法


本文实例讲述了C#读取系统字体颜色与大小的方法。分享给大家供大家参考。具体分析如下:

首先,说到字体、颜色,我们应该想到System.Drawing命名空间

先说说获取系统字体的方法:

在System.Drawing命名空间下有个FontFamily类,其下有个静态属性:Families(返回的是一个 FontFamily对象数组)

注:System.Drawsing.FontFamily是一个密封类。

而在System.Drawing.Text命名空间下有个InstalledFontCollection类,其下也有个属性:Families,不过此时不是静态属性。

注:System.Drawing.InstalledFontCollection也是一个密封类。

现在分别用这两个东东来获取一下:

FontFamily获取:

//前台有个familyList(DropDownList控件)
for(int i=0;i<FontFamily.Families.Length;i++)
{
  familyList.Items.Add(FontFamily.Families[i].Name);
}

第一种方法简单吧。

第二种方法:InstalledFontCollection

InstalledFontCollection ifc=new InstalledFontCollection();
foreach(FontFamily ff in ifc.Families)
{
 familyList2.Items.Add(ff.Name);
}

这个也简单 ^_^

获取系统已安装的颜色:

打开MSDN,你会发现,System.Drawing下有个KnownColor的枚举,其中就列出了N多颜色值哦,现在我们把它读出来~~

//System.Drawing.KnownColor
string[] colors=Enum.GetNames(typeof(System.Drawing.KnownColor);
foreach(string color in colors)
{ 
 ListItem list=new ListItem(color);
 list.Attributes.Add("style","color:"+color);
 colorList.Items.Add(list);
}

获取字体大小:

字体大小应该也和颜色一样有个枚举存储。但此时,它却在System.Web.UI.WebControls下了,大名叫:FontSize

代码如下:

//System.Web.UI.WebControls.FontSize
string[] sizes=Enum.GetName(typeof(System.Web.UI.WebControls.FontSize));
foreach(string size in sizes)
{
 sizeList.Items.Add(size);
}

随便提一下:Enum.GetNames(Type)返回的是一个字体串数组,而Enum.GetValues(Type)返回的是Array对象。

希望本文所述对大家的C#程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3