ARTICLE

System Pens and System Brushes in GDI+

Posted by Mahesh Chand Articles | GDI+ & Graphics February 10, 2010
In this article I will explain about working with brushes and pens in GDI+.
Reader Level:
Download Files:
 

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

System pens and system brushes are pens and brushes that are used to create system colors. In this section we will discuss how to create and use system pens and brushes.

There are two ways to create system pens and brushes. First, you can create pens and brushes using the SystemColors class. SystemColors represents the system colors in GDI+, providing static properties for system colors, such as ActiveBorder and ControlText. The second way to create systems pens and brushes use the SystemPens and SystemBrushes classes.

For performance reasons, it is a good idea to use the SystemPens and SystemBrushes classes rather than creating pens and brushes by using the SystemColors class.

System Pens

The SystemPens class represents a pen created with the system colors. This class has a static property for each system color that represents the system pen with that particular color. Table 4.16 lists the properties of the SystemPens class.

The SystemPens class also provides a method FormSystemColor that creates a Pen object from a Color structure. To create a system pen, we pass a SystemColors object. The following code shows how to use the FromSystemColor method:

            Pen pn = SystemPens.FromSystemColor(SystemColors.HotTrack);

TABLE 4.16: SystemPens properties

Property

Description

ActiveCaptionText

Pen with active window's title bar color

Control

Pen with control color

ControlDark

Pen with the shadow color of a 3D element

ControlDarkDark

Pen with the dark shadow color of a 3D element

ControlLight

Pen with the light color of a 3D element

ControlLightLight

Pen with the highlight color of a 3D element

ControlText

Pen with the control text color

GrayText

Pen with disabled color

Highlight

Pen with highlighting

HighlightText

Pen with highlighted text color

InactiveCaptionText

Pen with inactive title bar color

InfoText

Pen with the color of the text of a ToolTip

MenuText

Pen with the color of a menu's text

WindowFrame

Pen with the color of a window frame

WindowText

Pen with the color of the text in the client area of a window

System Brushes

The SystemBrushes class represents a Brush object using the system colors. All properties of SystemBrushes are static read-only properties. Table 4.17 describes these properties.

TABLE 4.17: SystemBrushes properties

Property

Description

ActiveBorder

Brush object with the color of the active window's border

ActiveCaption

Brush object with the background color of the active window's title bar

ActiveCaptionText

Brush object with the color of the text active window's title bar

AppWorkspace

Brush object with the color of the application work space

Control

Brush object with the face color of a 3D element

ControlDark

Brush object with the shadow color of a 3D element

ControlDarkDark

Brush object with the dark shadow color of a 3D element

ControlLight

Brush object with the light color of a 3D element

ControlLightLight

Brush object with the highlight color of a 3D element

ControlText

Brush object with the color of text in a 3D element

Desktop

Brush object with the color of the desktop

Highlight

Brush object with the color of the background of selected items

HighlightText

Brush object with the color of the text of selected items

HotTrack

Brush object with the color used to designate a hot tracked item

InactiveBorder

Brush object with the color of an inactive window's title bar

InactiveCaption

Brush object with the color of the background of an inactive window's title bar

Info

Brush object with the color of the background of a ToolTip

Menu

Brush object with the color of a menu's background

ScrollBar

Brush object with the color of the background of a scroll bar

Window

Brush object with the color of the background in the client area of a window

WindowText

Brush object with the color of the text in the client area of a window


Note: The MSDN documentation states that the SystemBrushes properties return a SolidBrush object, but that the statement is not quite accurate. These properties return a Brush object that must be cast to a SolidBrush object. If you run the code without casting them, the compiler throws an error.

The SystemBrushes class also provides a FromSystemColor method, which creates a Brush Object from a specified system color. The following code shows how to use the FromSystemMethod:

            SolidBrush brush = (SolidBrush)SystemBrushes.FromSystemColor(SystemColors.ActiveCaption);

Disposing of System Pens and Brushes

You cannot dispose of system pens and brushes. If you try to dispose of them, GDIF+ generates an error because these objects belong to the system

Listing 4.26 uses SystemBrushes and SystemPens object to draw two lines and rectangle.

Listing 4.26: Using the SystemBrushes and SystemPens classes

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 SystemPensSystemBrushes
{
    public partial class Form1 : Form
    {
        public Form1()
        {
             InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            //Create a pen using SystemPens
            Pen pn = SystemPens.FromSystemColor(SystemColors.HotTrack);

            //Create a brush using SystemBrushes
            SolidBrush brush = (SolidBrush)SystemBrushes.FromSystemColor(SystemColors.ActiveCaption);

            //Draw lines and rectangles
            g.DrawLine(pn, 20, 20, 20, 100);
            g.DrawLine(pn, 20, 20, 100, 20);
             g.FillRectangle(brush, 30, 30, 50, 50);

            //YOU CAN'T DISPOSE OF SYSTEM PENS AND 
            //BRUSHES.IF YOU TRY, GDI+ WILL GENERATE 
            //AN ERROR.
            //pn.Dispose ();
            //brush.Dispose ();
        }
    }
}

Figure 4.31 shows the output from Listing 4.26.

Figure 4.31.jpg

FIGURE 4.31: Using system pens and system brushes

Conclusion

Hope the article would have helped you in understanding how to draw a line chart 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.

COMMENT USING