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

添加人:McJeremy零级(68分)   添加时间:2008-05-27    阅读次数:692  收藏此教程
首先,说到字体、颜色,我们应该想到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 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发动就完成了吧。。。

====
原文地址:http://www.cnblogs.com/McJeremy/archive/2008/05/27/1208200.html
1页 第1上一页1下一页
相关的教程: 如何读取系统字体、颜色、大小?
收藏此教程

当前平均分: 0.0(2 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: