.NET中利用反射功能遍曆系統預定義的畫刷和顏色

來源:互聯網
上載者:User

昨天晚上在做GDI+的一個圖形程式,想試試系統中的哪個預定義顏色比較適合作為人臉的顏色,

結果猶豫了半天沒選好。如果做個小程式直接把各種預定義的顏色一行一行展示出來那不就方便了!

 

後面參考了一篇關於反射的文章,感覺利用反射可以達到我們的目標。

 

參考的是這篇文章:

http://download.csdn.net/source/1184896 (第02個小程式:遍曆畫筆(FlipThroughTheBrushes.cs))

 

程式比較短小,就是利用Brushes這個類型做反射,遍曆裡面的所有的域,然後一行一行畫在我們的主視窗上。

 

下面是整個小程式。(做的過程中試了下用VS2005的命令列程式做編譯,然後用EditPlus碼代碼,感覺這樣挺能練習對語言的熟練度的,呵呵)

 

代碼

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
namespace ShowColor
{
struct BrushWithName
{
public Brush Value;
public String Name;
}
public class ShowColor : Form
{
public static void Main(string[] args)
{
Application.Run(new ShowColor());
}
public ShowColor()
{
this.Text = "ShowColor程式";
this.ClientSize = new Size(800, 600);
this.Paint += new PaintEventHandler(this.PaintForm);
this.SizeChanged += new EventHandler(this.RefreshForm);
}
private void RefreshForm(object sender, EventArgs e)
{
this.Invalidate();
}

private void PaintForm(object sender, PaintEventArgs e)
{
PropertyInfo[] props;
props = typeof(Brushes).GetProperties();
const int numGroup = 4;
BrushWithName[][] brushGroups = new BrushWithName[numGroup][];
int brushCount = 0;
int brushEachGroup = props.Length / numGroup;
for (int i = 0; i < numGroup; ++i)
{
BrushWithName[] brushSubGroup;
brushSubGroup = new BrushWithName[brushEachGroup];
for (int j = 0; j < brushSubGroup.Length && brushCount < props.Length; ++j, ++brushCount)
{
brushSubGroup[j].Value = (Brush)props[brushCount].GetValue(null, null);
brushSubGroup[j].Name = props[brushCount].Name;
}
brushGroups[i] = brushSubGroup;
}
Size clientSize = this.Size;
Size blockSize = new Size(clientSize.Width / numGroup, clientSize.Height / brushEachGroup);
Point location = new Point();
for (int i = 0; i < brushGroups.Length; ++i)
{
for (int j = 0; j < brushGroups[i].Length; ++j)
{
Graphics g = e.Graphics;
if (brushGroups[i][j].Value == null) continue;
g.FillRectangle(brushGroups[i][j].Value, new Rectangle(location, blockSize));
if (blockSize.Height > 10)
{
Font font = new Font("宋體", 10);
string output = brushGroups[i][j].Name;
g.DrawString(output, font, Brushes.Black, location);
}
location.Offset(0, blockSize.Height);
}
location = new Point(location.X + blockSize.Width, 0);
}
}
}
}

 

 

這裡主要都是做GDI+的畫圖。我特意用了下二級數組,因為以前我做類似的任務都喜歡用List泛型,感覺還是要多熟練下多維陣列的用法。

 

不過List泛型裡的ForEach方法我還是覺得蠻過癮,因為用匿名委託感覺就像是在做函數式編程~

 

下面是程式的輸出畫面~在各種平台裡運行這東西應該顯示效果都不會差太遠吧~

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.