Using System Colors


At some point, you might have changed your Windows theme, or perhaps you changed the image or color of your desktop. What you might 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, choose Properties from the shortcut menu to display the Display Properties dialog box, and then click the Appearance tab (see Figure 18.1). To change the color for a specific item, you click the Advanced button, select the color attribute to change, and then choose a new color (if you're running a version of Windows other than Windows XP, your dialog box might be slightly different).

Figure 18.1. The Display Properties dialog box enables you to 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 application, it should also match its appearance to your selection. If you had to write code to manage this behavior, you'd have to write a lot of code, and you'd be justified in avoiding the whole mess. However, making an application adjust its appearance to match the user's system color selections is actually trivial, so there's no reason not to do it. For the most part, it's automatic with controls that you add to a form.

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 18.2). If you wanted to ensure that the color of a button matches the user's system color, for example, you would assign the System Color Control to the BackColor property of the Button control. Table 18.3 lists the most common system colors you can use. For a complete list, consult the online Help.

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


Table 18.3. System Colors

Enumeration

Description

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

Control

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

ControlDark

The color of shadows on a 3D element.

ControlLight

The color of 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 disabled or dimmed.

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.

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.

Menu

The color of the menu background.

MenuText

The color of the menu text.

Window

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


When a user changes a system color using the Display Properties dialog box, Visual 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. Fortunately, when you create new forms and when you add controls to forms, Visual C# automatically assigns the proper system color to the appropriate properties, so you don't usually have to muck with them.

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 enables you to draw custom interface elements that match the user's system colors, for example. Be aware, however, that if you do draw with system colors, Visual 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. In addition, if you apply system colors to properties that aren't usually assigned system colors, you run the risk of displaying odd color combinations, such as black on black, depending on the user's color settings.

By the Way

Users don't just change their system colors for aesthetic purposes. I work with a programmer who is color-blind. 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 might make using your program unnecessarily difficult, or even impossible, for someone with color-blindness or visual acuity issues.





Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net