本篇内容介绍了“C# 枚举Color并展示各种颜色效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
本文主要介绍了C# 枚举Color并展示各种颜色效果,分享给大家,具体如下:
本方法枚举Color以展示各颜色效果,方便为控件选择合适的颜色。本例需要在窗体项目中使用,通过创建ListView控件展示,代码如下:
private void displayColor() { Control listControl = this.Controls["listView_Color"]; if (listControl != null) { listControl.Visible = true; return; } ListView listView_Color = new ListView(); System.Windows.Forms.ColumnHeader columnHeaderA = new System.Windows.Forms.ColumnHeader(); System.Windows.Forms.ColumnHeader columnHeaderB = new System.Windows.Forms.ColumnHeader(); System.Windows.Forms.ColumnHeader columnHeaderC = new System.Windows.Forms.ColumnHeader(); System.Windows.Forms.ColumnHeader columnHeaderD = new System.Windows.Forms.ColumnHeader(); System.Windows.Forms.ColumnHeader columnHeaderE = new System.Windows.Forms.ColumnHeader(); System.Windows.Forms.ColumnHeader columnHeaderF = new System.Windows.Forms.ColumnHeader(); columnHeaderA.Width = 200; listView_Color.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { columnHeaderA, columnHeaderB, columnHeaderC, columnHeaderD, columnHeaderE, columnHeaderF }); string[] strArr = new string[6]; System.Array colorsArray = Enum.GetValues(typeof(System.Drawing.KnownColor)); for (int i = 0; i < colorsArray.Length; i++) { string colorName = colorsArray.GetValue(i).ToString(); //if (colorName.Contains("Green")) { System.Drawing.Color color = System.Drawing.Color.FromName(colorName); for (int j = 0; j < 6; j++) strArr[j] = colorName; ListViewItem li = new ListViewItem(strArr); li.ForeColor = color; listView_Color.Items.Add(li); li = new ListViewItem(strArr); li.BackColor = color; listView_Color.Items.Add(li); li = new ListViewItem(strArr); li.BackColor = color; li.ForeColor = System.Drawing.Color.White; listView_Color.Items.Add(li); } } this.Controls.Add(listView_Color); listView_Color.BringToFront(); listView_Color.Dock = DockStyle.Fill; listView_Color.View = System.Windows.Forms.View.Details; columnHeaderF.Width = listView_Color.DisplayRectangle.Width - columnHeaderA.Width - columnHeaderB.Width - columnHeaderC.Width - columnHeaderD.Width - columnHeaderE.Width; listView_Color.Name = "listView_Color"; } private void hideColor() { Control listControl = this.Controls["listView_Color"]; if(listControl!=null) listControl.Visible = false; }
“C# 枚举Color并展示各种颜色效果”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!