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




Active window border color


Active window title bar background color


Active window title bar text color


Multiple-document interface (MDI) workspace background color


Control background color


3D control shadow color


3D control dark shadow color


3D control highlight color


3D control light highlight color


Text color of controls


Window desktop color


Disabled text color


Highlighted text background color


Highlighted text color


Hot track color


Inactive window border color


Inactive window caption bar color


Inactive window caption bar text color


ToolTip background color


ToolTip text color


Menu background color


Menu text color


Background color of scroll bars


Background color of window


Thin window frame color


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
            Pen pn1 = SystemPens.FromSystemColor

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()

        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
            Pen pn1 = SystemPens.FromSystemColor
            Pen pn2 = SystemPens.FromSystemColor
            Pen pn3 = SystemPens.FromSystemColor

             //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

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


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;


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


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