枚举Color结构及SystemColors类中的颜色

// ----------------------------------------------------------------
//
// EnumColors.cs, Sat 2004.03.13, by dingdong
//
// 程序描述: 枚举Color结构及SystemColors类中的颜色
//
// 在System.Drawing命名空间中,Color结构有140个静态只读属性,它们是
// 从AliceBlue到YellowGreen(按字母顺序排列)的实际颜色名称。只有少数
// 名称(例如Magenta和Fuchsia)表示相同的颜色;其余的大多数都表示唯一
// 的颜色。Color结构还有第141个属性,它被称为Transparent,表示一种
// 透明的颜色。
//
// SystemColors类的26个只读属性(每一个属性都返回一个Color对象)反映了
// Windows的配色方案,被称为“用户的首选颜色”。Windows使用这些颜色
// 为不同的用户界面组件着色。
//
// ----------------------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

namespace AA.TEST
{
  class EnumColors : Form
  {           //个数 宽 高       空
    const int n = 6, xl = 125, yl = 30, sp = 4;
    const int i0 = (int)KnownColor.Transparent;
    const int i1 = (int)KnownColor.YellowGreen;
    const int i2 = (int)KnownColor.ActiveBorder;
    const int i3 = (int)KnownColor.WindowText;
    const int plx1 = 10, plx2 = 60, plx3 = 110, plx4 = 160, plx5 = 250;
    const int ply1 = 20, ply2 = 40, ply3 = 70;
    const int lw = 40, lh = 20, sh = 255;
    TabControl t = new TabControl();
    Button Ok = new Button();
    TextBox ta,tr,tg,tb;
    Label la,lr,lg,lb;
    Label ltext,lARGB,lbox;
    VScrollBar sa,sr,sg,sb;  
    
    static void Main()
    {
      Application.Run(new EnumColors());
    }
 
    public EnumColors()
    {
      TabPage p1,p2,p3;
      
      this.Text = "枚举颜色";
      this.Size = new Size(450,420);
      this.Icon= new Icon(GetType(), "pbrush.ico");
      t.Dock= DockStyle.Fill;     //停靠在边上.
      t.Parent = this;

      p1 = new TabPage();
      p2 = new TabPage();
      p3 = new TabPage();
      p1.Text = "Windows颜色";
      p2.Text = "系统颜色";  
      p3.Text = "自定义颜色";
      CreatePage(i0, i1, p1);
      CreatePage(i2, i3, p2);
      CreatPage3(p3);
    }
    private void CreatPage3(TabPage p)
    {
      p.Parent = t;
       
      Ok.Text = "应用";
      Ok.Location = new Point(plx5, 330);
      Ok.Size = new Size(2*lw, lh+5);
      Ok.Parent = p;
      this.AcceptButton = Ok;

      //垂直滚动条设置
      sa = new VScrollBar();
      sr = new VScrollBar();
      sg = new VScrollBar();
      sb = new VScrollBar();
      sa.Parent = p;
      sr.Parent = p;
      sg.Parent = p;
      sb.Parent = p;
      sa.Location = new Point(plx1+10,ply3);
      sr.Location = new Point(plx2+10,ply3);
      sg.Location = new Point(plx3+10,ply3);
      sb.Location = new Point(plx4+10,ply3);
      sa.Height = sh;
      sr.Height = sh;
      sg.Height = sh;
      sb.Height = sh;

      sa.Minimum = 0;
      sr.Minimum = 0;
      sg.Minimum = 0;
      sb.Minimum = 0;
      sa.Maximum = 264;
      sr.Maximum = 264;
      sg.Maximum = 264;
      sb.Maximum = 264;
      sa.Value = 255;
     // 标签设置     
      la = new Label();
      lr = new Label();
      lg = new Label();
      lb = new Label();
      la.Parent = p;
      lr.Parent = p;
      lg.Parent = p;
      lb.Parent = p;
      la.Text = "透明";
      lr.Text = "   R";
      lg.Text = "   G";
      lb.Text = "   B";
      la.Location = new Point(plx1,ply1);
      lr.Location = new Point(plx2,ply1);
      lg.Location = new Point(plx3,ply1);
      lb.Location = new Point(plx4,ply1);
      la.Size = new Size(lw,lh);
      lr.Size = new Size(lw,lh);
      lg.Size = new Size(lw,lh);
      lb.Size = new Size(lw,lh);

      //输入框设置
      ta = new TextBox();
      tr = new TextBox();
      tg = new TextBox();
      tb = new TextBox();
      ta.Parent = p;
      tr.Parent = p;
      tg.Parent = p;
      tb.Parent = p;
      ta.Location = new Point(plx1,ply2);     //位置
      tr.Location = new Point(plx2,ply2);
      tg.Location = new Point(plx3,ply2);
      tb.Location = new Point(plx4,ply2);
      ta.Size = new Size(lw,lh);             //大小
      tr.Size = new Size(lw,lh);
      tg.Size = new Size(lw,lh);
      tb.Size = new Size(lw,lh);

      //输出框设置
      ltext = new Label();
      lARGB = new Label();
      lbox = new Label();
      ltext.Parent = p;
      lARGB.Parent = p;
      lbox.Parent = p;
      lARGB.BorderStyle = BorderStyle.Fixed3D;
      lbox.BorderStyle = BorderStyle.Fixed3D;
      ltext.Location = new Point(plx5,ply1);
      lARGB.Location = new Point(plx5,ply2);
      lbox.Location = new Point(plx5,ply3);  
      ltext.Text = "ARGB值(Hex)";
      ltext.Size = new Size(2*lw,lh);
      lARGB.Size = new Size(3*lw,lh);
      lbox.Size = new Size(3*lw,12*lh);
      
      setcolor();
      Ok.Click += new EventHandler(Ok_Click);
      sa.ValueChanged += new EventHandler(Ok_Click);
      sr.ValueChanged += new EventHandler(Ok_Click);
      sg.ValueChanged += new EventHandler(Ok_Click);
      sb.ValueChanged += new EventHandler(Ok_Click);
      ta.Validated += new EventHandler(Input_Text);
      tr.Validated += new EventHandler(Input_Text);
      tg.Validated += new EventHandler(Input_Text);
      tb.Validated += new EventHandler(Input_Text);

    }

    void Input_Text(object sender, System.EventArgs e)
    {
      int a,r,g,b;
      try {
        a = int.Parse(ta.Text);
        r = int.Parse(tr.Text);
        g = int.Parse(tg.Text);
        b = int.Parse(tb.Text);
      }
      catch
      {
        MessageBox.Show("兄弟,输入有错啊!");
        setcolor();
        return;
      }
      if (a < 0) a = 0;
      if (a > 255) a = 255;
      if (r < 0) r = 0;
      if (r > 255) r = 255;
      if (g < 0) g = 0;
      if (g > 255) g = 255;
      if (b < 0) b = 0;
      if (b > 255) b = 255;
      sa.Value = a;
      sr.Value = r;
      sg.Value = g;
      sb.Value = b;
      setcolor();
    }
    void Ok_Click(object sender, System.EventArgs e)  
    {
      setcolor();
    }
    
    void setcolor()
    {
      Color c = new Color();
      if (sa.Value > 255) sa.Value = 255;
      if (sr.Value > 255) sr.Value = 255;
      if (sg.Value > 255) sg.Value = 255;
      if (sb.Value > 255) sb.Value = 255;
      lARGB.Text = string.Format("{0:X2}{1:X2}{2:X2}{3:X2}",sa.Value,sr.Value,sg.Value,sb.Value);
      c = Color.FromArgb(sa.Value,sr.Value,sg.Value,sb.Value);
      lbox.BackColor = c;
       
      ta.Text = string.Format("{0}", sa.Value);
      tr.Text = string.Format("{0}", sr.Value);
      tg.Text = string.Format("{0}", sg.Value);
      tb.Text = string.Format("{0}", sb.Value);
    }

    
    private void CreatePage(int i0, int i1, TabPage p)
    {
      int N = i1 - i0 + 1;
      TextBox [] label = new TextBox[N];
      
      p.Parent = t;         //p是t 的控件
      p.AutoScroll = true;  //自动滚动条
      for (int i = 0; i <= i1 - i0; i++)
      {
        KnownColor kc = (KnownColor)(i + i0);
        Color c = Color.FromKnownColor(kc);
        label[i] = new TextBox();
        label[i].ReadOnly = true;
        label[i].Parent = p;
        label[i].Multiline = true;
        label[i].Size = new Size(xl, yl);
        label[i].Location = new Point((i%n)*(xl+sp)+sp, (i/n)*(yl+sp)+sp);
        label[i].BorderStyle = BorderStyle.Fixed3D;
        if (i > 0) label[i].BackColor = c;
        if (i > 0) label[i].ForeColor = c.R<128||c.G<128||c.B<128 ? Color.White : Color.Black;
        label[i].Text = string.Format("{0}\r\n{1:X8}", kc, c.ToArgb());
      }
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值