| How to Set Windows System Colors Using API and Visual BasicID: Q82158 
 | 
This article describes how to use the GetSysColor and SetSysColors API functions to set the system colors for various parts of the display in Microsoft Windows. This allows you to change the Windows display programmatically, instead of using the Windows Control Panel.
Windows maintains an internal array of 19 color values that it uses to
paint the different parts of the Windows display. Changing any of
these values will affect all windows for all applications running
under Windows. Note that the SetSysColors routine only changes the
internal system list. This means that any changes made using
SetSysColors will only be valid for the current Windows session. To
make these changes permanent, you need to change the [COLORS] section
of the Windows initialization file, WIN.INI.
For more information on modifying the Windows initialization file
programmatically, query on the following words in the Microsoft
Knowledge Base:
GetProfileString and WriteProfileString
Declare Function GetSysColor Lib "User" (ByVal nIndex%) As Long
Declare Sub SetSysColors Lib "User" (ByVal nChanges%,
                                     lpSysColor%,
                                     lpColorValues&) 
Parameter         Definition
-------------------------------------------------------------
nIndex%           Specifies the display element whose color
                  is to be retrieved. See the list below to
                  find the index value for the corresponding
                  display element.
nChanges%         Specifies the number of system colors to
                  be changed.
lpSysColor%       Identifies the array of integer indexes
                  that specify the elements to be changed.
lpColorValues&    Identifies the array of long integers that
                  contain the new RGB color values for each
                  element to be changed. 
Windows.H Definition  Value  Description
-------------------------------------------------------
COLOR_SCROLLBAR         0    Scroll-bar gray area
COLOR_BACKGROUND        1    Desktop
COLOR_ACTIVECAPTION     2    Active window caption
COLOR_INACTIVECAPTION   3    Inactive window caption
COLOR_MENU              4    Menu background
COLOR_WINDOW            5    Window background
COLOR_WINDOWFRAME       6    Window frame
COLOR_MENUTEXT          7    Text in menus
COLOR_WINDOWTEXT        8    Text in windows
COLOR_CAPTIONTEXT       9    Text in caption, size box,
                             scroll bar arrow box
COLOR_ACTIVEBORDER      10   Active window border
COLOR_INACTIVEBORDER    11   Inactive window border
COLOR_APPWORKSPACE      12   Background color of multiple
                             document interface (MDI)
                             applications
COLOR_HIGHLIGHT         13   Items selected item in a
                             control
COLOR_HIGHLIGHTTEXT     14   Text of item selected in a
                             control
COLOR_BTNFACE           15   Face shading on push button
COLOR_BTNSHADOW         16   Edge shading on push button
COLOR_GRAYTEXT          17   Grayed (disabled) text. This
                             color is set to 0 if the
                             current display driver does not
                             support a solid gray color.
COLOR_BTNTEXT           18   Text on push buttons 
   Control         Name      Property Setting
   ------------------------------------------------------------
   Command button  Command1  Caption = "Change all Colors"
   Command button  Command2  Caption = "Change selected Colors"
 
   (In Visual Basic version 1.0 for Windows, set the CtlName
    Property for the above objects instead of the Name property.)
 
   Declare Function GetSysColor Lib "User" (ByVal nIndex%) As Long
   ' Enter the following Declare statement as one, single line:
   Declare Sub SetSysColors Lib "User" (ByVal nChanges%, lpSysColor%,
      lpColorValues&)
   Const COLOR_BACKGROUND = 1
   Const COLOR_ACTIVECAPTION = 2
   Const COLOR_WINDOWFRAME = 6
   Dim SavedColors(18) As Long
 
   Sub Form_Load ()
      ' Save current system colors:
      For i% = 0 To 18
         SavedColors(i%) = GetSysColor(i%)
      Next i%
   End Sub
 
   Sub Form1_Unload ()
      ' Restore system colors:
      ReDim IndexArray(18) As Integer
      For i% = 0 To 18
         IndexArray(i%) = i%
      Next i%
      SetSysColors 19, IndexArray(0), SavedColors(0)
   End Sub
 
   Sub Command1_Click ()
      ' Change all display elements:
      ReDim NewColors(18) As Long
      ReDim IndexArray(18) As Integer
      For i% = 0 to 18
         NewColors(i%) = QBColor(Int(16 * Rnd))
         IndexArray(i%) = i%
      Next i%
      SetSysColors 19, IndexArray(0), NewColors(0)
   End Sub
 
   Sub Command2_Click ()
      ' Change desktop, window frames, and active caption:
      ReDim NewColors(18) As Long
      ReDim IndexArray(18) As Integer
      For i% = 0 to 18
         NewColors(i%) = QBColor(Int(16 * Rnd))
         IndexArray(i%) = i%
      Next i%
      SetSysColors 19, IndexArray(0), NewColors(0)
   End Sub
 Additional query words: 2.00 3.00
Keywords          : 
Version           : 
Platform          : 
Issue type        : Last Reviewed: June 3, 1999