WPF最大的特點就是酷炫的外觀,在學習過程中經常看見各種漸變窗體。作為幾乎沒做過美工的程序員,我對各種顏色的argb值不熟,顏色的英文單詞也只認識部分。為了不至於每次都用Colors點出顏色再隨機挑選看效果。寫了個小程序展示System.Windows.Media.Colors中定義的141中顏色:
前台代碼:
<Window x:Class="IOC.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <WrapPanel Name="wp"></WrapPanel> </Window>
后台代碼:
/// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); IniWindow(); IniWrapPanel(); } /// <summary> /// 創建各種顏色的Lable,用以展示。 /// </summary> /// <param name="lblColor">要創建的Label的顏色</param> /// <returns></returns> public static Label Createlbl(Color lblColor) { Label lbl = new Label(); lbl.Height = 30; lbl.Width = 100; SolidColorBrush scb = new SolidColorBrush(lblColor); lbl.Background = scb; return lbl; } /// <summary> /// 初始化WrapPanel,其內容是各色標簽。 /// </summary> public void IniWrapPanel() { Type t = typeof(Colors); PropertyInfo[] pInfo = t.GetProperties(); foreach (PropertyInfo pi in pInfo) { Color c = (Color)ColorConverter.ConvertFromString(pi.Name); Label lbl = Createlbl(c); lbl.Content = pi.Name; this.wp.Children.Add(lbl); } } /// <summary> /// 初始化窗體,以合理的尺寸顯示各種顏色。 /// </summary> public void IniWindow() { this.Title = "ColorPresentation"; this.ResizeMode = ResizeMode.NoResize; this.Height = 600; this.Width = 820; this.Content = wp; } }
展示效果:

