What's New
Q & A
Tip Jar
Tutorial: Background Color
This brief tutorial by Mike Klein explains how to use the OleTranslateColor API function to translate a VB color constant into an RGB color. It then shows how to use SendMessage to change a TreeView control's background color property even though a TreeView doesn't have a BackColor property.


Some controls, such as the TreeView, do not have a property that lets us change the background color.

It's the programmer's responsibility to use the system constants faithfully rather than hard-coding specific colors. It's also important to understand the difference between the system colors assigned to Visual Basic properties and the RGB colors passed to most GDI functions. For reasons known only to historians, system colors have the type OLE_COLOR. If you give a property this type in a UserControl, it will display the standard color picker in the Properties window. If you make the property type Long, it will be just another integer property. On the other hand, if you pass a system color to an API function that expects an RGB color, you probably won't like the result. To complicate things further, there's a third kind of color, an index into the current color palette. You probably won't find much use for these colors unless you go far beyond the meager palette techniques.

If you have a system color but want an RGB color, you can use the OleTranslateColor API function to find the real color behind the color. This is one of the most hideously complicated simple functions in the API. The TranslateColor wrapper function makes it easy to do common operations.

The subroutine requires two parameters:

  • the handle of the control
  • the Visual Basic system color to change the backcolor to
The first call made is to the API function that translates the Visual Basic system color into its RGB equivalent. The second call is made to change the control's backcolor.

Object Definitions

Private Const GWL_STYLE = -16&
Private Const TVM_SETBKCOLOR = 4381&
Private Const TVM_GETBKCOLOR = 4383&
Private Const TVS_HASLINES = 2&
Private Const CLR_INVALID = &HFFFF

Public Declare Function SendMessage Lib "user32" _
                         Alias "SendMessageA" _
                         (ByVal hWnd As Long, _
                          ByVal wMsg As Long, _
                          ByVal wParam As Long, _
                          lParam As Any) As Long

Public Declare Function GetWindowLong Lib "user32" _
                         Alias "GetWindowLongA" _
                         (ByVal hWnd As Long, _
                          ByVal nIndex As Long) As Long

Public Declare Function SetWindowLong Lib "user32" _
                         Alias "SetWindowLongA" _
                         (ByVal hWnd As Long, _
                          ByVal nIndex As Long, _
                          ByVal dwNewLong As Long) As Long

Public Declare Function OleTranslateColor Lib "oleaut32" _
                         (ByVal clr As OLE_COLOR, _
                          ByVal hPal As Long, _
                          dwRGB As Long) As Long

Translate a Visual Basic system color into an RGB color

'*  TranslateColor
'*  This function takes a VB color constant and
'*    translates it into its RGB equivalent.

Public Function TranslateColor(ByVal clrColor As OLE_COLOR, _
    Optional hPalette As Long = 0) As Long

If OleTranslateColor(clrColor, hPalette, TranslateColor) Then
  TranslateColor = CLR_INVALID
End If

End Function

Change the backcolor of a control

'*  ChangeBackgroundColor
'*  This subroutine changes a window's background color
'*    using a series of API calls.

Public Sub ChangeBackgroundColor(ByVal hWnd As Long, _
                                 ByVal longColor As Long)

Dim longStyle As Long

'* Change color

Call SendMessage(hWnd, TVM_SETBKCOLOR, 0, _
    ByVal TranslateColor(longColor))

'* Reset style so lines display properly
'* (for TreeView control only)

longStyle = GetWindowLong(hWnd, GWL_STYLE)
Call SetWindowLong(hWnd, GWL_STYLE, longStyle - TVS_HASLINES)
Call SetWindowLong(hWnd, GWL_STYLE, longStyle)

End Sub

Click here to download an example program.

Subscribe to the VB Helper newsletter
Copyright © 1997-2001 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
www.vb-helper.com/tut10.htm Updated