Csharp/C Sharp by API/System.Drawing/Color

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Color.AliceBlue

  
//ColorGuide.cs
/*
   This Program will generate all the colors that are supported in C# according 
   to the Name....
   I expected from you a better version of this program
     If you can , then inform me how you made it
   e.g. you make a dll file where the whole color array is stored then 
   your coding lines will  decrease. I tried that way but the trial was 
   in vane....
*/
using System;
using System.Windows.Forms;
using System.Drawing;
public class CreateMyPanel : Form
{  
  Color[] color = new Color[]{
                   Color.AliceBlue,    Color.AntiqueWhite,   Color.Aqua,   Color.Aquamarine,
                   Color.Azure,        Color.Beige,          Color.Bisque, Color.Black,
                   Color.BlanchedAlmond,Color.Blue,          Color.BlueViolet,Color.Brown,
                   Color.BurlyWood,    Color.CadetBlue,      Color.Chartreuse,Color.Chocolate,
                   Color.Coral,        Color.Cornsilk,Color.Crimson,
                   Color.Cyan,         Color.DarkBlue,       Color.DarkCyan,Color.DarkGoldenrod,
                   Color.DarkGray,     Color.DarkGreen,      Color.DarkKhaki,Color.DarkMagenta,
                   Color.DarkOliveGreen,Color.DarkOrange,Color.DarkOrchid,Color.DarkRed,
                   Color.DarkSalmon,Color.DarkSeaGreen,Color.DarkSlateBlue,Color.DarkSlateGray,
                   Color.DarkTurquoise,Color.DarkViolet,Color.DeepPink,Color.DeepSkyBlue,
                   Color.DimGray,Color.DodgerBlue,Color.Firebrick,Color.FloralWhite,
                   Color.ForestGreen,Color.Fuchsia,Color.Gainsboro,Color.GhostWhite,
                   Color.Gold,Color.Goldenrod,Color.Gray,Color.Green,Color.GreenYellow,
                   Color.Honeydew,Color.HotPink,Color.IndianRed,Color.Indigo,
                   Color.Ivory,Color.Khaki,Color.Lavender,Color.LavenderBlush,
                   Color.LawnGreen,Color.LemonChiffon,Color.LightBlue,Color.LightCoral,
                   Color.LightCyan,Color.LightGoldenrodYellow,Color.LightGray,
                   Color.LightGreen,Color.LightPink,Color.LightSalmon,Color.LightSeaGreen,
                   Color.LightSkyBlue,Color.LightSlateGray,Color.LightSteelBlue,
                   Color.LightYellow,Color.Lime,Color.LimeGreen,Color.Linen,
                   Color.Magenta,Color.Maroon,Color.MediumAquamarine,Color.MediumBlue,
                   Color.MediumOrchid,Color.MediumPurple,Color.MediumSeaGreen,
                   Color.MediumSlateBlue,Color.MediumSpringGreen,Color.MediumTurquoise,
                   Color.MediumVioletRed,Color.MidnightBlue,Color.MintCream,Color.MistyRose,
                   Color.Moccasin,Color.NavajoWhite,Color.Navy,Color.OldLace,
                   Color.Olive,Color.OliveDrab,Color.Orange,Color.OrangeRed,
                   Color.Orchid,Color.PaleGoldenrod,Color.PaleGreen,Color.PaleTurquoise,
                   Color.PaleVioletRed,Color.PapayaWhip,Color.PeachPuff,Color.Peru,
                   Color.Pink,Color.Plum,Color.PowderBlue,Color.Purple,Color.Red,
                   Color.RosyBrown,Color.RoyalBlue,Color.SaddleBrown,Color.Salmon,
                   Color.SandyBrown,Color.SeaGreen,Color.SeaShell,Color.Sienna,Color.Silver,
                   Color.SkyBlue,Color.SlateBlue,Color.SlateGray,Color.Snow,
                   Color.SpringGreen,Color.SteelBlue,Color.Tan,Color.Teal,
                   Color.Thistle,Color.Tomato,Color.Transparent,Color.Turquoise,
                   Color.Violet,Color.Wheat,Color.White,Color.WhiteSmoke,Color.Yellow,
                   Color.YellowGreen
                 };
  private Panel panel1 = new Panel(); 
  private Label[] col = new Label[140];
  public CreateMyPanel() 
  {     
    // Initialize the Panel control.
    panel1.Location = new Point(ClientRectangle.Left + 5,ClientRectangle.Top + 5);
    panel1.Size = new Size(ClientRectangle.Right-5, ClientRectangle.Bottom-5);  
    panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.Controls.Add(panel1); // Add the Panel control to (inside) the form.  
    // Initalize the Label controls.  
    int ystart = ClientRectangle.Top;
    for(int j=0; j<140; j++)
      col[j] = new Label();
    for(int i = 0; i<140; i++)
    {
      col[i].Size = new Size(ClientRectangle.Right, 20);  
      col[i].Font = new System.Drawing.Font("Comic Sans MS",10,FontStyle.Bold);  
      col[i].ForeColor = Color.Black;
      if(col[i].Equals(Color.Black) == true)
      {
          col[i].ForeColor = Color.White;
      }
      col[i].Text = color[i].ToString();
      col[i].Location = new Point(ClientRectangle.Left,ystart);
      col[i].BackColor = color[i];
      col[i].BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;  
      panel1.Controls.Add(col[i]);  // Add the Label controls to (inside) the Panel.
      if((col[i].Location.Y > panel1.Location.Y))
      {
        panel1.AutoScroll = true;   
      }  
      ystart += 20;
    }
    this.Size = new Size(315, 300);
    this.Text = "A Color Guide - JAYANT";
    this.MaximizeBox = false;
//    this.BorderStyle = FormBorderStyle.FixedDialog;
    this.StartPosition = FormStartPosition.CenterScreen;
  }
  public static void Main() 
  {
    Application.Run(new CreateMyPanel());
  }
}  
/* To Compile :----
csc /r:System.dll /r:System.Drawing.dll /r:System.Windows.Forms.dll /r:Microssft.Win32.InterOp.dll /out:ColorGuide.exe Colorguide.cs
*/


Color.Chocolate

  
using System;
using System.Drawing;
using System.Windows.Forms;
   
class PaintEvent
{
     public static void Main()
     {
          Form form   = new Form();
          form.Text   = "Paint Event";
          form.Paint += new PaintEventHandler(MyPaintHandler);
   
          Application.Run(form);
     }
     static void MyPaintHandler(object objSender, PaintEventArgs pea)
     {
          Graphics graphics = pea.Graphics;
   
          graphics.Clear(Color.Chocolate);
     }
}


Color.FromArgb(olor.Blue)

  
  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);
      Color c1 = Color.FromArgb(100, Color.Blue);
      Color c2 = Color.FromArgb(50, Color.Green);
      g.FillEllipse(Brushes.Red, 20, 20, 80, 80);
      g.FillRectangle(new SolidBrush(c1), 60, 80, 60, 60);
      Point[] pa = new Point[] {
                        new Point(150, 40), 
                        new Point(90, 40), 
                        new Point(90, 120)};
      g.FillPolygon(new SolidBrush(c2), pa);
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


Color.FromArgb(77, color.R, color.G, color.B)

  
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
  public Form1() {
        InitializeComponent();
  }
  private void Form1_Paint(object sender, PaintEventArgs e)
  {
    Bitmap bitmap = new Bitmap("winter.jpg");
    TextureBrush brush = new TextureBrush(bitmap);
    e.Graphics.FillRectangle(brush, ClientRectangle);
        bitmap.Dispose();
    Color color = Color.Yellow;
    int penWidth = 80;
    Pen opaquePen = new Pen(color, penWidth);
    e.Graphics.DrawLine(opaquePen, 0, 50, 200, 20);
        opaquePen.Dispose();
    Color semiTransparentColor = Color.FromArgb(128, color.R, color.G, color.B);
    Pen semiTransparentPen = new Pen(semiTransparentColor, penWidth);
    e.Graphics.DrawLine(semiTransparentPen, 0, 200, 200, 140);
        semiTransparentPen.Dispose();
    Color veryTransparentColor = Color.FromArgb(77, color.R, color.G, color.B);
    Pen veryTransparentPen = new Pen(veryTransparentColor, penWidth);
    e.Graphics.DrawLine(veryTransparentPen, 0, 350, 200, 260);
        veryTransparentPen.Dispose();
  
    Brush transparentBrush = new SolidBrush(semiTransparentColor);
    e.Graphics.DrawString("www.nfex.ru", new Font("Verdana", 36, FontStyle.Bold),
      transparentBrush, 80, 150);
  }
  private void InitializeComponent()
  {
    this.SuspendLayout();
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Name = "Form1";
    this.Text = "Alpha Blending";
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    this.ResumeLayout(false);
  }

  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }
}


Color.FromArgb (int r, int g, int b)

  
using System;
using System.Drawing;
using System.Windows.Forms; 
public class ColorChips : Form {
  public ColorChips() {
    Size = new Size(300,300);
    Text = "Color Chips";
  }
  protected override void OnPaint(PaintEventArgs e)   {
    Graphics g = e.Graphics;
    int h = DisplayRectangle.Height;
    int w = DisplayRectangle.Width;
    Random r = new Random();
    for (int i = 0; i < 10; i++){
      for (int j = 0; j < 10; j++) {
        Color color = Color.FromArgb (r.Next(256), r.Next(256), r.Next(256));
        Brush brush = new SolidBrush(color); 
        g.FillRectangle(brush, i*w/10, j*h/10, w/10, h/10);
      } 
    }  
    base.OnPaint(e);
  }
  static void Main() {
    Application.Run(new ColorChips());
  }
}


Color.FromKnownColor

  

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace ColorChanger
{
    public class ColorChanger : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Label lblSaturation;
        internal System.Windows.Forms.Label lblHue;
        internal System.Windows.Forms.Label lblBrightness;
        internal System.Windows.Forms.Label Label1;
        internal System.Windows.Forms.ListBox lstColors;
        private System.ruponentModel.Container components = null;
        public ColorChanger()
        {
            InitializeComponent();
        }
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        private void InitializeComponent()
        {
            this.lblSaturation = new System.Windows.Forms.Label();
            this.lblHue = new System.Windows.Forms.Label();
            this.lblBrightness = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.lstColors = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            this.lblSaturation.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.lblSaturation.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblSaturation.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.lblSaturation.Location = new System.Drawing.Point(264, 56);
            this.lblSaturation.Name = "lblSaturation";
            this.lblSaturation.Size = new System.Drawing.Size(136, 20);
            this.lblSaturation.TabIndex = 9;
            this.lblSaturation.Text = " Saturation";
            this.lblHue.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.lblHue.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblHue.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.lblHue.Location = new System.Drawing.Point(264, 32);
            this.lblHue.Name = "lblHue";
            this.lblHue.Size = new System.Drawing.Size(136, 20);
            this.lblHue.TabIndex = 8;
            this.lblHue.Text = " Hue";
            this.lblBrightness.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.lblBrightness.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblBrightness.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.lblBrightness.Location = new System.Drawing.Point(264, 8);
            this.lblBrightness.Name = "lblBrightness";
            this.lblBrightness.Size = new System.Drawing.Size(136, 20);
            this.lblBrightness.TabIndex = 7;
            this.lblBrightness.Text = " Brightness";
            this.Label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Label1.Location = new System.Drawing.Point(8, 8);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(200, 20);
            this.Label1.TabIndex = 6;
            this.Label1.Text = " Choose a Background Color:";
            this.lstColors.Location = new System.Drawing.Point(8, 36);
            this.lstColors.Name = "lstColors";
            this.lstColors.Size = new System.Drawing.Size(200, 238);
            this.lstColors.TabIndex = 5;
            this.lstColors.SelectedIndexChanged += new System.EventHandler(this.lstColors_SelectedIndexChanged);
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(472, 290);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.lblSaturation,
                                                                          this.lblHue,
                                                                          this.lblBrightness,
                                                                          this.Label1,
                                                                          this.lstColors});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "ColorChanger";
            this.Text = "Color Changer";
            this.Load += new System.EventHandler(this.ColorChanger_Load);
            this.ResumeLayout(false);
        }
        #endregion
        [STAThread]
        static void Main() 
        {
            Application.Run(new ColorChanger());
        }
        private void ColorChanger_Load(object sender, System.EventArgs e)
        {
            string[] colorNames;
            colorNames = System.Enum.GetNames(typeof(KnownColor));
            lstColors.Items.AddRange(colorNames);
        }
        private void lstColors_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            KnownColor selectedColor;
            selectedColor = (KnownColor)System.Enum.Parse(typeof(KnownColor), lstColors.Text);
            
            this.BackColor = System.Drawing.Color.FromKnownColor(selectedColor);
            
            lblBrightness.Text = "Brightness = " +this.BackColor.GetBrightness().ToString();
            lblHue.Text = "Hue = " + this.BackColor.GetHue().ToString();
            lblSaturation.Text = "Saturation = " + this.BackColor.GetSaturation().ToString();
        }
    }
}


Color.FromName

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;

public class MainClass{
   public static void Main(){
    Color currColor = Color.FromName("Blue");
   }
}


Color.LightGray

   
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace AddControls
{
  /// <summary>
  /// Summary description for FormAddControls.
  /// </summary>
  public class FormAddControls : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ruponentModel.Container components = null;
    public FormAddControls()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.SuspendLayout();
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(38, 200);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(216, 20);
      this.textBox1.TabIndex = 1;
      this.textBox1.Text = "";
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(48, 240);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(80, 24);
      this.button1.TabIndex = 2;
      this.button1.Text = "Add Item";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      this.button1.MouseEnter += new System.EventHandler(this.Buttons_OnMouseEnter);
      this.button1.MouseLeave += new System.EventHandler(this.Buttons_OnMouseLeave);
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(160, 240);
      this.button2.Name = "button2";
      this.button2.Size = new System.Drawing.Size(96, 24);
      this.button2.TabIndex = 3;
      this.button2.Text = "Cancel";
      this.button2.Click += new System.EventHandler(this.button2_Click);
      this.button2.MouseEnter += new System.EventHandler(this.Buttons_OnMouseEnter);
      this.button2.MouseLeave += new System.EventHandler(this.Buttons_OnMouseLeave);
      // 
      // listBox1
      // 
      this.listBox1.Location = new System.Drawing.Point(38, 32);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(216, 147);
      this.listBox1.TabIndex = 0;
      // 
      // FormAddControls
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button2,
                                      this.button1,
                                      this.textBox1,
                                      this.listBox1});
      this.Name = "FormAddControls";
      this.Text = "FormAddControls";
      this.ResumeLayout(false);
    }
    #endregion
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new FormAddControls());
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
      if (textBox1.Text == "")
        return;
      string strAdd = textBox1.Text;
      if (listBox1.FindString (strAdd, -1) < 0)
      {
        listBox1.Items.Add (strAdd);
        textBox1.Text = "";
        textBox1.Focus ();
        return;
      }
      MessageBox.Show ("\"" + strAdd + "\" is already in the list box", "Duplicate");
    }
    private void button2_Click(object sender, System.EventArgs e)
    {
      Application.Exit();
    }
    private void Buttons_OnMouseEnter(object sender, System.EventArgs e)
    {
      Button btn = (Button) sender;
      btn.BackColor = Color.LightGray;
    }
    private void Buttons_OnMouseLeave(object sender, System.EventArgs e)
    {
      Button btn = (Button) sender;
      btn.BackColor = SystemColors.Control;
    }
  }
}


Color.Tomato

  
  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class MainForm : System.Windows.Forms.Form
  {
    private System.ruponentModel.Container components;
    public MainForm()
    {
      InitializeComponent();
      BackColor = Color.Tomato;
      Opacity = 0.5d;
      Text = "www.nfex.ru";
      Cursor = Cursors.WaitCursor;
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Form1";
    }
    [STAThread]
    static void Main() 
    {
      Application.Run(new MainForm());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      g.DrawString("www.nfex.ru", 
        new Font("Times New Roman", 20), 
        new SolidBrush(Color.Black), 40, 10);
    }
  }


Color.White

   

using System;
using System.Drawing;
using System.Windows.Forms;
   
class InheritTheForm: Form
{
     public static void Main()
     {
          InheritTheForm form = new InheritTheForm();
          form.Text = "Inherit the Form";
          form.BackColor = Color.White;
   
          Application.Run(form);
     }
}