To use the SystemColors class, just as with the other classes in this chapter, first create a new instance of the class, and then work with its properties. For example, to change the background color of buttons, you could use code like this:
Dim sc As New SystemColors
Dim lngColor As Long
lngColor = sc.ButtonFace
' Set the button background to be red.
sc.ButtonFace = 255
By the way, should you try this experiment, you may be surprised; changing the ButtonFace property also changes the color of many other objects in Windows, including scrollbars and menu bars. Unfortunately, there’s no support in the API for controlling the Windows color scheme, so you’ll need to work with each color separately. It’s also unfortunate that we could find no documentation on the interrelations between various screen artifacts and the system colors—you’ll find that changing one of the properties of the SystemColors object may, in fact, change the color of a seemingly unrelated object. Experiment carefully when using these properties in applications.
If you intend to assign the system color values to elements of your application’s user interface, don’t assign the value you retrieved from properties of the SystemColors object. Although you can do this if you like, it will defeat your purpose. If a user changes the system colors, you want your interface to automatically alter itself to match the new colors. If you hard-code a value you retrieve at design time, your interface cannot magically alter itself.
If, instead, you choose a value from Table 9.24, your user interface will always match the settings chosen in the Windows color scheme. Use the values in the first column in your VBA code and the values in the second column in property sheets.
Table 9.24: System Color Constants for Use in the User Interface
VBA Constant | Value for Property Sheet | Description |
vbScrollBars | &H80000000 | Scrollbar color |
vbDesktop | &H80000001 | Desktop color |
vbActiveTitleBar | &H80000002 | Color of the title bar for the active window |
vbInactiveTitleBar | &H80000003 | Color of the title bar for the inactive window |
vbMenuBar | &H80000004 | Menu background color |
vbWindowBackground | &H80000005 | Window background color |
vbWindowFrame | &H80000006 | Window frame color |
vbMenuText | &H80000007 | Color of text on menus |
vbWindowText | &H80000008 | Color of text in windows |
vbTitleBarText | &H80000009 | Color of text in caption, size box, and scroll arrow |
vbActiveBorder | &H8000000A | Border color of active window |
vbInactiveBorder | &H8000000B | Border color of inactive window |
vbApplicationWorkspace | &H8000000C | Background color of multiple document interface (MDI) applications |
vbHighlight | &H8000000D | Background color of items selected in a control |
vbHighlightText | &H8000000E | Text color of items selected in a control |
vbButtonFace | &H8000000F | Color of shading on the face of command buttons |
vbButtonShadow | &H80000010 | Color of shading on the edge of command buttons |
vbGrayText | &H80000011 | Grayed (disabled) text |
vbButtonText | &H80000012 | Text color on push buttons |
vbInactiveCaptionText | &H80000013 | Color of text in an inactive caption |
vb3DHighlight | &H80000014 | Highlight color for 3-D display elements |
vb3DDKShadow | &H80000015 | Darkest shadow color for 3-D display elements |
vb3DLight | &H80000016 | Second lightest 3-D color after vb3DHighlight |
vbInfoText | &H80000017 | Color of text in ToolTips |
vbInfoBackground | &H80000018 | Background color of ToolTips |