How to Set Windows System Colors Using API and Visual Basic

ID Number: Q82158

1.00

WINDOWS

Summary:

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.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

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

To use the GetSysColor and SetSysColors functions within a Visual

Basic application, you must first declare them in either the Global

section or within the Declarations section of your Code window.

Declare the Function statement as follows:

Declare Function GetSysColor Lib "User" (ByVal nIndex%) As Long

Declare 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 predefined

constants found in the WINDOWS.H file supplied with Microsoft Windows

version 3.0 Software Development Kit (SDK). The corresponding value is

the value placed in the lpSysColor% array.

List of System Color Indexes

----------------------------

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

The following is an example of how to set the system colors for

different parts of the Windows display:

1. Start Visual Basic, or from the File menu, choose New Project (ALT,

F, N) if Visual Basic is already running. Form1 is created by

default.

2. Create the following controls for Form1:

Control CtlName Property Setting

------- ------- ----------------

Command button Command1 Caption = "Change all Colors"

Command button Command2 Caption = "Change selected Colors"

3. Add the following code to the general Declarations section of

Form1:

Declare Function GetSysColor Lib "User" (ByVal nIndex%) As Long

Declare Sub SetSysColors Lib "User" (ByVal nChanges%,

lpSysColor%,

lpColorValues&)

' Note: The above declaration must be on one line.

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 F5, to run the program.

Choosing the Change All Colors button will cause all the different

parts of the Windows display to be assigned a randomly generated

color. Choosing the Change Selected Elements button will cause only

the desktop, active window caption, and window frames to be assigned a

random color. To restore the original system colors, double-click the

Control-menu box to end the application.

Reference(s):

"Programming Windows: The Microsoft Guide to Writing Applications for

Windows 3," Charles Petzold, Microsoft Press, 1990

"Microsoft Windows Software Development Kit: Reference Volume 1,"

version 3.0

"Microsoft Windows Software Development Kit: Guide to Programming,"

version 3.0.

WINSDK.HLP file shipped with Microsoft Windows 3.0 Software

Development Kit

Additional reference words: 1.00 3.00