How To Change the TreeView Control Background Color

This article was previously published under Q178491
This article has been archived. It is offered "as is" and will no longer be updated.
This article illustrates how to change the background color of the TreeViewcontrol to fit your user interface requirements.
The Custom Controls Library file, comctl32.dll, version 4.70 or later, isrequired to change the TreeView background color. This file is available byinstalling Internet Explorer version 4.0 or later. Note that this file isnot currently redistributable. If you need this file, then you must requireyour users to install Internet Explorer 4.0. Please review the informationregarding the use and redistribution rights for Microsoft Internet Explorerlocated at:

Should you have questions regarding the licensing of Internet Explorer,please contact

To change the background color of a TreeView control, you can call thefollowing Windows API functions:
  • SendMessage - sends the specified message to a window or windows.
  • GetWindowLong - retrieves information about the specified window.
  • SetWindowLong - changes an attribute of the specified window.
The next section shows you how to create a sample project that demonstrateshow to use these functions to change the background color. The sectionassumes you have the correct version of comctl32.dll installed on yourcomputer.

NOTE: If the display of your copy of Windows is set for 256 colors or less,using a dithered color as your background color will produce unsatisfactoryresults. This is because the TreeView and most other Windows controlscannot display text over a dithered background. Change the last parameterof the call to SendMessage to RGB(200, 255, 200) to view this effect.

NOTE: Microsoft recommends that YOU DO not use this method of changing the background color with the mscomctl.ocx version of the TreeView control. If you do, it is at your own risk.

Steps to Create Sample Project

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Add a reference to the Microsoft Common Controls by completing the following steps:
  3. From the Project menu, click Components.
  4. From the Controls list, check Microsoft Windows Common Controls.
  5. Click OK to close the Components dialog box.
  6. Add a TreeView control and a CommandButton to Form1.
  7. Copy the following code to the Code window of Form1 form:
          Option Explicit      Private Declare Function SendMessage Lib "User32" _         Alias "SendMessageA" _         (ByVal hWnd As Long, _         ByVal wMsg As Long, _         ByVal wParam As Long, _         lParam As Long) As Long      Private Declare Function GetWindowLong Lib "User32" _         Alias "GetWindowLongA" _         (ByVal hWnd As Long, _         ByVal nIndex As Long) As Long      Private Declare Function SetWindowLong Lib "User32" _         Alias "SetWindowLongA" _         (ByVal hWnd As Long, _         ByVal nIndex As Long, _         ByVal dwNewLong As Long) As Long      Private Const GWL_STYLE = -16&      Private Const TVM_SETBKCOLOR = 4381&      Private Const TVM_GETBKCOLOR = 4383&      Private Const TVS_HASLINES = 2&      Dim frmlastForm As Form      Private Sub Form_Load()          Dim nodX As Node          Set nodX = TreeView1.Nodes.Add(, , "R", "Root")          Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C1", "Child 1")          Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C2", "Child 2")          Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C3", "Child 3")          Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C4", "Child 4")          nodX.EnsureVisible = tvwTreelinesText ' Style 4.          TreeView1.BorderStyle = vbFixedSingle      End Sub      Private Sub Command1_Click()         Dim lngStyle As Long          Call SendMessage(TreeView1.hWnd, _                           TVM_SETBKCOLOR, _                           0, _                           ByVal RGB(255, 0, 0))  'Change the background                                                  'color to red.      ' Now reset the style so that the tree lines appear properly         lngStyle = GetWindowLong(TreeView1.hWnd, GWL_STYLE)         Call SetWindowLong(TreeView1.hWnd, _                           GWL_STYLE, _                           lngStyle - TVS_HASLINES)         Call SetWindowLong(TreeView1.hWnd, GWL_STYLE, lngStyle)      End Sub					
  8. On the Run menu, click Start or press the F5 key to start the program.
  9. Click the CommandButton. The TreeView background color changes to red.
Platform SDK Documentation on the following functions:
  • SendMessage
  • GetWindowLong
  • SetWindowLong

Article ID: 178491 - Last Review: 12/05/2015 08:15:12 - Revision: 2.2

Microsoft Visual Basic 5.0 Learning Edition, Microsoft Visual Basic 6.0 Learning Edition, Microsoft Visual Basic 5.0 Professional Edition, Microsoft Visual Basic 6.0 Professional Edition, Microsoft Visual Basic 5.0 Enterprise Edition, Microsoft Visual Basic 6.0 Enterprise Edition

  • kbnosurvey kbarchive kbhowto KB178491