Use the predefined system colors in C#

The System.Drawing.SystemColors enumeration defines the standard system colors. These include colors for such items as active objects, highlighted text, captions, and window frames.

The following code fragment shows how the program's Paint event handler displays color samples by calling the DrawColorSample method.

private void Form1_Paint(object sender, PaintEventArgs e)
{
int y = 10;

DrawColorSample(e.Graphics, ref y,
SystemColors.ActiveBorder, "ActiveBorder");
DrawColorSample(e.Graphics, ref y,
SystemColors.ActiveCaption, "ActiveCaption");
DrawColorSample(e.Graphics, ref y,
SystemColors.ActiveCaptionText, "ActiveCaptionText");

...

DrawColorSample(e.Graphics, ref y,
SystemColors.WindowText, "WindowText");

this.ClientSize = new Size(this.ClientSize.Width, y + 10);
}

The DrawColorSample method fills a rectangle with a color, displays its name, and then increments the y variable so the next color sample appears below this one.


// Display a color sample.
private void DrawColorSample(Graphics gr, ref int y, Color clr, string clr_name)
{
using (SolidBrush br = new SolidBrush(clr))
{
gr.FillRectangle(br, 10, y, 90, 10);
}
gr.DrawRectangle(Pens.Black, 10, y, 90, 10);
gr.DrawString(clr_name, this.Font, Brushes.Black, 110, y);
y += 15;
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.