本文整理汇总了C#中System.Drawing.Color.FromKnownColor方法的典型用法代码示例。如果您正苦于以下问题:C# Color.FromKnownColor方法的具体用法?C# Color.FromKnownColor怎么用?C# Color.FromKnownColor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Color
的用法示例。
在下文中一共展示了Color.FromKnownColor方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: KnownColorBrightnessExample1
public void KnownColorBrightnessExample1(PaintEventArgs e)
{
Graphics g = e.Graphics;
// Color structures. One is a variable used for temporary storage. The other
// is a constant used for comparisons.
Color someColor = Color.FromArgb(0);
Color redShade = Color.FromArgb(255, 200, 0, 100);
// Array to store KnownColor values that match the brightness of the
// redShade color.
KnownColor[] colorMatches = new KnownColor[15];
// Number of matches found.
int count = 0;
// Iterate through the KnownColor enums until 15 matches are found.
for (KnownColor enumValue = 0;
enumValue <= KnownColor.YellowGreen && count < 15; enumValue++)
{
someColor = Color.FromKnownColor(enumValue);
if (someColor.GetBrightness() == redShade.GetBrightness())
colorMatches[count++] = enumValue;
}
// Display the redShade color and its argb value.
SolidBrush myBrush1 = new SolidBrush(redShade);
Font myFont = new Font("Arial", 12);
int x = 20;
int y = 20;
someColor = redShade;
g.FillRectangle(myBrush1, x, y, 100, 30);
g.DrawString(someColor.ToString(), myFont, Brushes.Black, x + 120, y);
// Iterate through the matches that were found and display each color that
// Corresponds with the enum value in the array. also display the name of
// The KnownColor.
for (int i = 0; i < count; i++)
{
y += 40;
someColor = Color.FromKnownColor(colorMatches[i]);
myBrush1.Color = someColor;
g.FillRectangle(myBrush1, x, y, 100, 30);
g.DrawString(someColor.ToString(), myFont, Brushes.Black, x + 120, y);
}
}
示例2: ColorChanger
//引入命名空间
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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.ComponentModel.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();
}
}
}