如何读取系统字体、颜色、大小?

首先,说到字体、颜色,我们应该想到System.Drawing命名空间(当然,纯属个人无稽之谈,火星访客也可以想到银河系空间去^_^)。
先说说获取系统字体的方法:
  在System.Drawing命名空间下有个FontFamily类,其下有个静态属性:Families(返回的是一个 FontFamily对象数组)
---注:System.Drawsing.FontFamily是一个密封类。
 而在System.Drawing.Text命名空间下有个InstalledFontCollection类,其下也有个属性:Families,不过此时不是静态属性。
---注:System.Drawing.InstalledFontCollection也是一个密封类。
现在分别用这两个东东来获取一下:
 1//FontFamily获取
 2//前台有个familyList(DropDownList控件)
 3for(int i=0;i<FontFamily.Families.Length;i++)
 4{
 5   familyList.Items.Add(FontFamily.Families[i].Name);
 6}

 7//第一种方法简单吧。
 8//第二种方法:InstalledFontCollection
 9InstalledFontCollection ifc=new InstalledFontCollection();
10foreach(FontFamily ff in ifc.Families)
11{
12  familyList2.Items.Add(ff.Name);
13}

14///也简单 ^_^

====获取系统已安装的颜色呢?
打开MSDN,你会发现,System.Drawing下有个KnownColor的枚举,其中就列出了N多颜色值哦,现在我们把它读出来~~
1//System.Drawing.KnownColor
2string[] colors=Enum.GetNames(typeof(System.Drawing.KnownColor);
3foreach(string color in colors)
4
5  ListItem list=new ListItem(color);
6  list.Attributes.Add("style","color:"+color);
7  colorList.Items.Add(list);
8}

=====获取字体大小:
 字体大小应该也和颜色一样有个枚举存储。但此时,它却在System.Web.UI.WebControls下了,大名叫:FontSize
代码如下:
1//System.Web.UI.WebControls.FontSize
2string[] sizes=Enum.GetName(typeof(System.Web.UI.WebControls.FontSize));
3foreach(string size in sizes)
4{
5  sizeList.Items.Add(size);
6}

///随便提一下:Enum.GetNames(Type)返回的是一个字体串数组,而Enum.GetValues(Type)返回的是Array对象。
---
这么简短的步骤,我想,火星人大概还等不及UFO发动就完成了吧。。。
posted @ 2008-05-27 10:43  mcjeremy  阅读(2224)  评论(0编辑  收藏  举报
------------------------------------ 心静似高山流水不动,心清若巫峰雾气不沾 ------------------------------------