Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSet the system's colors (window background, highlight, text, etc.)
DescriptionThis example shows how to set the system's colors (window background, highlight, text, etc.) in Visual Basic 6.
Keywordssystem color, color
CategoriesWindows, Graphics
 
When the form loads, it fills two collections with the system colors' names and numberic codes. It then displays the names and samples of the colors in labels.
 
Private Sub Form_Load()
Const XGAP = 240
Const YGAP = 30
Dim xspacing As Single
Dim yspacing As Single
Dim i As Integer
Dim X1 As Single
Dim X2 As Single
Dim Y As Single

    Set ColorNumbers = New Collection
    Set ColorNames = New Collection
    Set ColorValues = New Collection

    ' Load the color names and numbers.
    ColorNumbers.Add COLOR_ACTIVEBORDER
    ColorNames.Add "COLOR_ACTIVEBORDER"
    ColorNumbers.Add COLOR_ACTIVECAPTION
    ColorNames.Add "COLOR_ACTIVECAPTION"
    ...
    ColorNumbers.Add COLOR_WINDOWTEXT
    ColorNames.Add "COLOR_WINDOWTEXT"

    ' Get the color values.
    For i = 1 To ColorNames.Count
        ColorValues.Add GetSysColor(ColorNumbers(i))
    Next i

    ' Display the colors.
    yspacing = lblColor(0).Height + YGAP
    xspacing = lblName(0).Left + _
        lblName(0).Width - lblColor(0).Left + XGAP
    Y = lblName(0).Top
    X1 = lblName(0).Left
    X2 = lblColor(0).Left
    For i = 1 To ColorNames.Count
        Load lblName(i)
        With lblName(i)
            .Left = X1
            .Top = Y
            .Caption = ColorNames(i)
            .Visible = True
        End With

        Load lblColor(i)
        With lblColor(i)
            .Left = X2
            .Top = Y
            .BackColor = ColorValues(i)
            .Visible = True
        End With

        If i Mod 10 = 0 Then
            X1 = X1 + xspacing
            X2 = X2 + xspacing
            Y = lblName(0).Top
        Else
            Y = Y + yspacing
        End If
    Next i

    ' Size the form.
    Width = lblName(ColorNames.Count).Left + _
        lblName(ColorNames.Count).Width
    Height = Height - ScaleHeight + _
        lblColor(10).Top + _
        lblColor(10).Height + 120
End Sub
 
Later iwhen the user clicks on a color sample, the program displays a color selection dialog. If the user picks a color and clicks OK, the program uses the SetSysColors API function to change the corresponding system color.
 
' Let the user select the color for this value.
Private Sub lblColor_Click(Index As Integer)
Dim color_number As Long
Dim color_value As Long

    dlgSelectColor.Color = ColorValues(Index)
    dlgSelectColor.CancelError = True

    ' Present the color selection dialog.
    On Error Resume Next
    dlgSelectColor.ShowColor
    If Err.Number <> 0 Then Exit Sub

    ' Change the color.
    color_number = ColorNumbers(Index)
    color_value = dlgSelectColor.Color
    SetSysColors 1, color_number, color_value
    lblColor(Index).BackColor = color_value
End Sub
 
Note that changing the system colors may be very annoying to users. You may at least want to save the colors before you change them so you can restore them later.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated