Creating the SystemColors Class

The SystemColors class was one of the simplest in this chapter to create. It relies on only two API functions: GetSysColor and SetSysColors. GetSysColor retrieves a single system color, given a constant representing the item to be retrieved. For example, the following excerpt from the SystemColors class retrieves the background color for ToolTips:

Property Get TooltipBackground() As Long
    ' ToolTip background color system color.
    TooltipBackground = GetSysColor(COLOR_INFOBK)
End Property

Setting a system color requires a tiny bit more effort because the SetSysColors function is capable of setting multiple colors at once. The code in Listing 9.13 sets the background color for ToolTips, using the SystemColors’ SetColor procedure. This procedure calls the SetSysColors API procedure, which allows you to send as many colors as you like. SetColor is sending only a single color, but it could work with a group of colors at a time. In this case, it passes 1 as the first parameter, indicating that it’s supplying only a single color. The second parameter indicates which color it’s sending (COLOR_INFOBK, a predefined constant, indicates that this color is the background for ToolTips), and the third supplies the new color.

Listing 9.13: Setting a System Color Uses the SetColor Procedure

Property Let TooltipBackground(Value As Long)
    ' ToolTip background color system color.
    Call SetColor(COLOR_INFOBK, Value)
End Property
Private Sub SetColor(lngID As Long, lngValue As Long)
    Call SetSysColors(1, lngID, lngValue)
End Sub

You may find that you’d rather have the SystemColors class allow you to set a number of new colors before sending the information to Windows. This will make the update faster because Windows won’t try to repaint the screen after each color change. To do this, you’ll need to create an array of colors, one for each constant in the SystemColors class module. Then, in each Property Get, modify the value in the appropriate row of the array rather than calling SetColor. Finally, you’ll need to add a new method of the class that you’ll call when you’re ready to update all the colors. This method will call SetSysColors, passing the number of items in the array (25, if you use the colors in the class module), an array containing the numbers 0 through 24, and an array containing all the new colors. That will cause Windows to set all 25 colors at once. One more tip: make sure you initialize the color array with all the current colors, in the Initialize event of the class. Otherwise, when you set the new colors, all the colors you haven’t modified will contain 0 (black), and your Windows environment will become very difficult to use.

© 1997 by SYBEX Inc. All rights reserved.