本文實例講述了C#讀取系統字體顏色與大小的方法。分享給大家供大家參考。具體分析如下:
首先,說到字體、顏色,我們應該想到System.Drawing命名空間
先說說獲取系統字體的方法:
在System.Drawing命名空間下有個FontFamily類,其下有個靜態屬性:Families(返回的是一個 FontFamily對象數組)
注:System.Drawsing.FontFamily是一個密封類。
而在System.Drawing.Text命名空間下有個InstalledFontCollection類,其下也有個屬性:Families,不過此時不是靜態屬性。
注:System.Drawing.InstalledFontCollection也是一個密封類。
現在分別用這兩個東東來獲取一下:
FontFamily獲?。?/strong>
//前臺有個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.KnownColorstring[] 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);}
獲取字體大?。?/strong>
字體大小應該也和顏色一樣有個枚舉存儲。但此時,它卻在System.Web.UI.WebControls下了,大名叫:FontSize
代碼如下:
//System.Web.UI.WebControls.FontSizestring[] 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#程序設計有所幫助。
新聞熱點
疑難解答