Home > Articles > Programming > Windows Programming

This chapter is from the book 

Using System Colors

At some point, you may have changed your Windows theme, or perhaps you changed the image or color of your desktop. What you may not be aware of is that Windows enables you to customize the colors of almost all Windows interface elements. The colors that Windows allows you to change are called system colors. To change your system colors, right-click the desktop and choose Properties from the shortcut menu to display the Display Properties dialog box, and then click the Appearance tab (see Figure 1). To change the color for a specific item, you can select the item from the Item drop-down list, or you can click the element in the top half of the tab.

NOTE

On Windows XP, you'll need to click Advanced on the Appearance tab to change your system colors.

Figure 1 The Display Properties dialog box lets you select the colors of most Windows interface elements.

When you change a system color using the Display Properties dialog box, all loaded applications should change their appearance to match your selection. In addition, when you start any new applications, they should also match their appearance to your selection. If you had to write code to manage this behavior, you would have to write a lot of code, and you would be justified in avoiding the whole thing. However, making an application adjust its appearance to match the user's system color selections is actually quite trivial; therefore, there's no reason not to do it.

To designate that an interface color should stay in sync with a user's system colors, you assign a system color to a color property of the item in question (see Figure 2). Table 3 lists the system colors you can use. For example, if you wanted to ensure that the color of a button matches the user's corresponding system color, you would assign the system color named Control to the BackColor property of the Button control.

Figure 2 System colors are assigned using the System palette tab.

Fortunately, when you create new forms and when you add controls to forms, C# automatically assigns the proper system color to the appropriate properties. Another good thing is that when a user changes a system color using the Display Properties dialog box, C# automatically updates the appearance of objects that use system colors; you don't have to write a single line of code to do this.

Be aware that you aren't limited to assigning system colors to their logically associated properties. You can assign system colors to any color property you want, and you can also use system colors when drawing. This allows you, for example, to draw custom interface elements that match the user's system colors. Be aware, however, that if you do draw with system colors, C# won't update the colors automatically when the user changes system colors; you would have to redraw the elements with the new system color.

NOTE

Users don't just change their system colors for aesthetic purposes. I work with a programmer who is colorblind. He's modified his system colors so that he can see things better on the screen. If you don't allow your applications to adjust to the color preferences of the user, you may make using your program unnecessarily difficult, or even impossible, for someone with color blindness.

Table 3β€”Properties of the SystemColors Class

Enumeration

Description

ActiveBorder

The color of the filled area of an active window border.

ActiveCaption

The color of the background of an active caption bar (title bar).

ActiveCaptionText

The color of the text of the active caption bar (title bar).

AppWorkspace

The color of the application workspace. The application workspace is the area in a multiple-document view that is not being occupied by child windows.

Control

The color of the background of push buttons and other 3D elements.

ControlDark

The color of shadows on a 3D element.

ControlDarkDark

The color of darkest shadows on a 3D element.

ControlLight

The color of highlights on a 3D element.

ControlLightLight

The color of lightest highlights on a 3D element.

ControlText

The color of the text on buttons and other 3D elements.

Desktop

The color of the Windows desktop.

GrayText

The color of the text on a user-interface element when it's unavailable.

Highlight

The color of the background of highlighted text. This includes selected menu items as well as selected text.

HighlightText

The color of the foreground of highlighted text. This includes selected menu items as well as selected text.

HotTrack

The color used to represent hot tracking.

InactiveBorder

The color of an inactive window border.

InactiveCaption

The color of the background of an inactive caption bar.

InactiveCaptionText

The color of the text of an inactive caption bar.

Info

The color of the background of the ToolTip.

InfoText

The color of the text of the ToolTip.

Menu

The color of the menu background.

MenuText

The color of the menu text.

ScrollBar

The color of the scrollbar background.

Window

The color of the background in the client area of a window.

WindowFrame

The color of the frame around a window.

WindowText

The color of the text in the client area of a window.


InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.