Chapter 34 -- Use System Colors

Chapter 34

Microsoft Windows maintains 28 system colors for painting various parts of the display. These system colors include colors settings for window text, backgrounds, and borders; dialog box text and backgrounds; shading of 3-D objects; and even tooltip text and backgrounds. By allowing the user to change the system colors, Windows provides a significant amount of flexibility in determining the appearance of the display.

You need to make sure your program correctly uses the system colors. Using the system colors correctly helps you display your program the way the user wants it displayed. It is also necessary to satisfy the Designed for Microsoft Windows logo requirements. In this chapter, I'll explore the implications of using the system colors in your code and give some suggestions for ways to make sure that you have done it correctly.

Let's start by looking at a typical dialog box screen. The window below is the Background tab of the Display Control Panel applet.

In the center of this tab is a computer monitor graphic that gives you a preview of your selections. Since this is a graphic of something other than a standard screen element, it's acceptable that the graphic is drawn using various shades of gray that are independent of the system colors. On the other hand, everything else in this tab should be drawn using system colors. To see if this tab is drawn correctly, try changing the system colors. If done right, the monitor graphic should always look the same but all other elements should change to reflect the new settings. Furthermore, the changes should happen immediately and completely. You shouldn't have to close and redisplay the dialog box to see the change.

Using the system colors correctly has the following implications: You cannot assume that window backgrounds are always white, that dialog boxes are always light gray, or that text is always black. You cannot assume that 3-D shading is always done with white and dark gray. In short, you really can't assume anything about colors—you must always use the GetSysColor and GetSysColorBrush functions to create GDI objects for painting screen elements. On the other hand, graphics that are independent of the system objects, such as the monitor graphic example above, should not change with the system colors.

TIP
Do not assume that standard screen elements are drawn using shades of gray. Always use the GetSysColor and GetSysColorBrush functions to draw such screen elements.

Be careful when deciding which system colors to use. Select the system color that most closely matches the description of what you are using the color for. Do not make the selection based on the actual color itself on your system, since the actual colors have no significance. Note that many of the system colors come in sets, such as window text (COLOR_WINDOWTEXT) and window background color (COLOR_WINDOW). You should always use these sets of colors as a group and never mix and match them. For example, suppose you are creating a custom slider control that has text on the slider. Going through the descriptions of the system colors, it's clear that you should use the button color (COLOR_BTNFACE) for the slider itself. What color should you use for the text? Applying the above rule, the only choice is button text color (COLOR_BTNTEXT), since no other selection is guaranteed to be legible against the button color.

TIP
Select the system colors based on their description, not their appearance. Do not mix and match system colors that are part of a set.

Windows sends the WM_SYSCOLORCHANGE message to all top-level windows when a change is made to a system color setting. These windows must forward the WM_SYSCOLORCHANGE message to the common controls (such as toolbars, list views, tree views, and tab controls), but forwarding to the basic controls (such as buttons and static text controls) is unnecessary. You also have to be careful how you manage GDI resources. Any bitmaps, brushes, and pens that use system colors that are kept in memory must be destroyed and re-created when your program receives the WM_SYSCOLORCHANGE message.

TIP
Handle the WM_SYSCOLORCHANGE message so that color changes happen immediately and completely.

You cannot go wrong by using the system colors. You don't have to worry about preventing users from selecting ugly appearance schemes. Choosing an attractive appearance scheme is the user's problem, not yours. Using system colors correctly for standard screen elements is never a problem—using fixed colors that the user cannot change is.

When you cannot use the system colors for a screen element (which should be rare), do not use the system colors at all. For example, suppose you have a window that simply isn't legible if the background color isn't white. In this case, you can choose not to use the window background system color, but also make sure that you don't use the system colors for anything else in that window. Use black for text, and do not use the system color for window text. Why? Because the user can set the window text color to be white or a light color, making the text illegible on the white background.



Developing User Interfaces for Microsoft Windows
Developing User Interfaces for Microsoft Windows
ISBN: 0735605866
EAN: 2147483647
Year: 2005
Pages: 334
Authors: Everett N McKay
BUY ON AMAZON

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