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#程序设计有所帮助。

(0)

相关推荐

  • C#取得随机颜色的方法

    本文实例讲述了C#取得随机颜色的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: public string GetRandomColor() {         Random RandomNum_First = new Random((int)DateTime.Now.Ticks);         //  对于C#的随机数,没什么好说的         System.Threading.Thread.Sleep(RandomNum_First.Next(50));    

  • C#及WPF获取本机所有字体和颜色的方法

    本文实例讲述了C#及WPF获取本机所有字体和颜色的方法.分享给大家供大家参考.具体如下: WPF 获取所有的字体: System.Drawing.Text.InstalledFontCollection font = new System.Drawing.Text.InstalledFontCollection(); System.Drawing.FontFamily[] array= font.Families; foreach (var v in array) { MessageBox.Sh

  • WPF实现渐变淡入淡出的登陆窗口效果

    本文实例讲述了WPF实现渐变淡入淡出的登陆窗口效果的方法.分享给大家供大家参考.具体实现方法如下: 1.实现原理 ① 利用UIElement.OpacityMask属性,用于改变对象区域的不透明度的画笔.可以使元素的特定区域透明或部分透明,从而实现比较新颖的效果. ② OpacityMask属性接受任何画刷,可利用LinearGradientBrush线性渐变画刷,通过对渐变画刷中各颜色点加以动画处理即可. 2.渐变淡入实现 渐变淡入效果,可通过事件触发器触发Loaded事件实现,所以可以仅用前

  • C#实现缩放字体的方法

    本文实例讲述了C#实现缩放字体的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace adv

  • C#在RichTextBox中显示不同颜色文字的方法

    本文实例讲述了C#在RichTextBox中显示不同颜色文字的方法.分享给大家供大家参考.具体实现方法如下: #region 日志记录.支持其他线程访问 public delegate void LogAppendDelegate(Color color, string text); /// <summary> /// 追加显示文本 /// </summary> /// <param name="color">文本颜色</param> /

  • C#通过重写Panel改变边框颜色与宽度的方法

    本文实例讲述了C#通过重写Panel改变边框颜色与宽度的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.ComponentModel; using System.Windows.Forms; using System.Drawing; namespace Imag

  • C# Winform使用扩展方法实现自定义富文本框(RichTextBox)字体颜色

    在利用C#开发Winform应用程序的时候,我们有可能使用RichTextBox来实现实时显示应用程序日志的功能,日志又分为:一般消息,警告提示和错误等类别.为了更好地区分不同类型的日志,我们需要使用不同的颜色来输出对应的日志,比如:一般消息为绿色,警告提示的用橙色,错误的用红色字体. 在原生Winform的RichTextBox中,是没有这种设置选项的.如需实现以上描述的功能,我们可以使用.NET的静态扩展方法来处理.实现扩展方法的类和方法本身都必须是静态的,如果你对扩展方法还不是太了解,建议

  • C#实现更改MDI窗体背景颜色的方法

    本文实例讲述了C#实现更改MDI窗体背景颜色的方法.分享给大家供大家参考.具体实现方法如下: /// <summary> /// 设置MDI背景 /// </summary> void RemoveMdiBackColor() { foreach (Control c in this.Controls) { if (c is MdiClient) { c.BackColor = this.BackColor; //颜色 c.BackgroundImage = this.Backgr

  • C#实现字体旋转的方法

    本文实例讲述了C#实现字体旋转的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace adv

  • C#中WPF使用多线程调用窗体组件的方法

    本文实例讲述了C#中WPF使用多线程调用窗体组件的方法.分享给大家供大家参考.具体如下: Thread thread=new Thread(new ThreadStart(TestThread)); thread.Start(); private void TestThread() { for (int i = 0; i < 11;i++ ) { Thread.Sleep(2000); this.listBox1.Dispatcher.Invoke(new Action(() => { thi

随机推荐