System Colors in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

The SystemColors class represents the Windows system colors; it provides 26 read-only properties, each of which returns a Color object. Table 5.1 lists the properties of the SystemColors class.

The following code snippet uses the SystemColors class to set colors of a few Window controls. In this code we set the background colors of a textbox, a radio button, and a button to inactive border, active caption, and control dark system colors, respectively.

            textBox1.BackColor = SystemColors.InactiveBorder;
            radioButton1.BackColor = SystemColors.ActiveCaption;
            button1.BackColor = SystemColors.ControlDarkDark;

If you're wondering whether you can create a brush or a pen from the SystemColors class to fill and draw shapes, curves, and text, the answer is, absolutely. The following code snippet uses SystemColors to create SolidBrush and Pen objects. This code creates a solid brush and a pen from active caption system and highlight text system colors, respectively.

            SolidBrush brush = new SolidBrush(SystemColors.ActiveCaption);
            Pen pn = new Pen(SystemColors.HighllightText);

TABLE 5.1 SystemColor Properties

Property

Description

ActiveBorder

Active window border color

ActiveCaption

Active window title bar background color

ActiveCaptionText

Active window title bar text color

AppWorkspace

Multiple-document interface (MDI) workspace background color

Control

Control background color

ControlDark

3D control shadow color

ControlDarkDark

3D control dark shadow color

ControlLight

3D control highlight color

ControlLightLight

3D control light highlight color

ControlText

Text color of controls

Desktop

Window desktop color

GrayText

Disabled text color

HighLight

Highlighted text background color

HeighlightText

Highlighted text color

HotTrack

Hot track color

InactiveBorder

Inactive window border color

InactiveCaption

Inactive window caption bar color

InactiveCaptionText

Inactive window caption bar text color

Info

ToolTip background color

InfoText

ToolTip text color

Menu

Menu background color

MenuText

Menu text color

ScrollBar

Background color of scroll bars

Window

Background color of window

WindowFrame

Thin window frame color

WindowText

Window text color

For performance reasons, GDI+ provides SystemPens and SystemBrushes classes, which should be used instead of creating a brush or pen from the SystemColors class. For example, the following method is advisable for creating system brushes and pens. This code snippet creates a solid brush and a pen from active caption and highlight text system colors, respectively.

            SolidBrush brush1 = (SolidBrush)SystemBrushes.FromSystemColor
            (SystemColors.ActiveCaption);
            Pen pn1 = SystemPens.FromSystemColor
            (SystemColors.HighlightText);

Listing 5.3 uses the SystemBrushes and SystemPens classes to create a SolidBrush object and three Pen objects, which are used later to draw and fill graphics objects. The solid brush is created from the active caption system color, and the three pens are created from highlight text, control light, and control dark system colors, respectively. Later the brush and pens are used to draw two lines, a rectangle, and an ellipse.

LISTING 5.3: Using SystemPens and SystemBrushes

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
     public partial class Form1 : Form
    {
        public Form1()
        {
             InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {          

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
             //Create a Graphics object
            Graphics g = this.CreateGraphics();

             //Create brushes and pens 
            SolidBrush brush1 = (SolidBrush)SystemBrushes.FromSystemColor
                (SystemColors.ActiveCaption);
            Pen pn1 = SystemPens.FromSystemColor
                (SystemColors.HighlightText);
            Pen pn2 = SystemPens.FromSystemColor
                (SystemColors.ControlLightLight);
            Pen pn3 = SystemPens.FromSystemColor
                (SystemColors.ControlDarkDark);

             //Draw and fill graphics objects
            g.DrawLine(pn1, 10, 10, 10, 200);
             g.FillRectangle(brush1, 60, 60, 100, 100);
            g.DrawEllipse(pn3, 20, 20, system-colors-in-gdi0, system-colors-in-gdi0);
            g.DrawLine(pn2, 10, 10, 200, 10);

             //Dispose of object
            g.Dispose();
        }
    }
}

Figure 5.3 shows the output from Listing 5.3. System colors were used to draw two lines, an ellipse, and a rectangle.

fig5.3.gif

Figure 5.3: Using system colors to draw graphics object

Note: When you create pens using SystemPens, you cannot modify the width or other properties of the pen .The code will compile but will throw an unhandled exception when executed .If you create a pen using SystemColors, however, you can modify its width like this:

            Pen pn = new Pen(SystemColors.HighlightText);
            Pn.Width = 4;

Conclusion

Hope the article would have helped you in understanding System Colors in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg

This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.