You are currently offline, waiting for your internet to reconnect

How to Set Windows System Colors Using API and Visual Basic

This article was previously published under Q82158
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
SUMMARY
This article describes how to use the GetSysColor and SetSysColors APIfunctions to set the system colors for various parts of the display inMicrosoft Windows. This allows you to change the Windows displayprogrammatically, instead of using the Windows Control Panel.
MORE INFORMATION
Windows maintains an internal array of 19 color values that it uses topaint the different parts of the Windows display. Changing any ofthese values will affect all windows for all applications runningunder Windows. Note that the SetSysColors routine only changes theinternal system list. This means that any changes made usingSetSysColors will only be valid for the current Windows session. Tomake these changes permanent, you need to change the [COLORS] sectionof the Windows initialization file, WIN.INI.

For more information on modifying the Windows initialization fileprogrammatically, query on the following words in the MicrosoftKnowledge Base:
GetProfileString and WriteProfileString

To use the GetSysColor and SetSysColors functions within a VisualBasic for Window application, you must first declare them in theDeclarations section of your Code window.

Declare the Function statement as follows:
Declare Function GetSysColor Lib "User" (ByVal nIndex%) As LongDeclare Sub SetSysColors Lib "User" (ByVal nChanges%,                                     lpSysColor%,                                     lpColorValues&)				

NOTE: Each Declare statement above must be written on one line.

The parameters are defined as follows:
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.				

The following system color indexes are defined using the predefinedconstants found in the WINDOWS.H file supplied with the MicrosoftWindows Software Development Kit (SDK). The corresponding value isthe value placed in the lpSysColor% array.

List of System Color Indexes

Windows.H Definition  Value  Description-------------------------------------------------------COLOR_SCROLLBAR         0    Scroll-bar gray areaCOLOR_BACKGROUND        1    DesktopCOLOR_ACTIVECAPTION     2    Active window captionCOLOR_INACTIVECAPTION   3    Inactive window captionCOLOR_MENU              4    Menu backgroundCOLOR_WINDOW            5    Window backgroundCOLOR_WINDOWFRAME       6    Window frameCOLOR_MENUTEXT          7    Text in menusCOLOR_WINDOWTEXT        8    Text in windowsCOLOR_CAPTIONTEXT       9    Text in caption, size box,                             scroll bar arrow boxCOLOR_ACTIVEBORDER      10   Active window borderCOLOR_INACTIVEBORDER    11   Inactive window borderCOLOR_APPWORKSPACE      12   Background color of multiple                             document interface (MDI)                             applicationsCOLOR_HIGHLIGHT         13   Items selected item in a                             controlCOLOR_HIGHLIGHTTEXT     14   Text of item selected in a                             controlCOLOR_BTNFACE           15   Face shading on push buttonCOLOR_BTNSHADOW         16   Edge shading on push buttonCOLOR_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				

The following is an example of how to set the system colors fordifferent parts of the Windows display:
  1. Start Visual Basic for Windows, or from the File menu, choose New Project (press ALT, F, N) if Visual Basic for Windows is already running. Form1 is created by default.
  2. Create the following controls for Form1:
       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.)						
  3. Add the following code to the general Declarations section of Form1:
       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						
  4. Add the following code to the Form_Load event procedure of Form1:
       Sub Form_Load ()      ' Save current system colors:      For i% = 0 To 18         SavedColors(i%) = GetSysColor(i%)      Next i%   End Sub						
  5. Add the following code to the Form_Unload event procedure of Form1:
       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						
  6. Add the following code to the Command1_Click event procedure of Form1:
       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						
  7. Add the following code to the Command2_Click event procedure of Form1:
       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						
  8. From the Run menu, choose Start, or press the F5 key, to run the program.
Choosing the Change All Colors button will cause all the different parts ofthe Windows display to be assigned a randomly generated color. Choosing theChange Selected Elements button will cause only the desktop, active windowcaption, and window frames to be assigned a random color. To restore theoriginal system colors, double-click the Control-menu box to end theapplication.
2.00 3.00
Properties

Article ID: 82158 - Last Review: 11/18/2003 15:42:18 - Revision: 2.0

  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 1.0 Standard Edition
  • KB82158
Feedback