System Pens and System Brushes

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 system pens and brushes uses 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.

4.5.1 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 methodFromSystemColorthat 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:

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

 

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

 

4.5.2 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 in the active window's title bar

AppWorkspace

Brush object with the color of the application workspace

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 border

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 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 FromSystemColor method:

 

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, GDI+ generates an error because these objects belong to the system.

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

Listing 4.26 Using the SystemBrushes and SystemPens classes

private void Form1_Paint(object sender, System.Windows.Forms.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. Using system pens and system brushes

Категории