September 23, 2018
Hot Topics:

VB Coding Tip: Treeview - Changing Colours

  • November 17, 2002
  • By Sam Huggill
  • Send Email »
  • More Articles »

This is the second part of the Treeview code series. This week we use API calls to change the background colour of the treeview control, and a few other settings.

'API Call for Sending the message
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 

'Constants for changing the treeview
Private Const GWL_STYLE = -16&
Private Const TVM_SETBKCOLOR = 4381&
Private Const TVM_GETBKCOLOR = 4383&
Private Const TVS_HASLINES = 2&
Private Const TV_FIRST As Long = &H1100
Private Const TVM_GETTEXTCOLOR As Long = _
                          (TV_FIRST + 32)
Private Const TVM_SETTEXTCOLOR As Long = _
                          (TV_FIRST + 30) 

Private Sub Form_Load()
'This is last weeks code
'Declare Variables
Dim mNode As Node
Dim i As Integer
'Populate the list box
List1.AddItem "Node 1"
List1.AddItem "Node 2"
List1.AddItem "Node 3"
'Add the primary node
Set mNode = TreeView1.Nodes.Add()
mNode.Text = "Primary Node"
'Add the nodes from the list box
For i = 0 To List1.ListCount - 1
Set mNode = TreeView1.Nodes.Add(1, tvwChild)
mNode.Text = List1.List(i)
Next i 

'This is this weeks code
'Send a message to the treeview telling it to
'change the background It uses an RGB colour setting
Call SendMessage(TreeView1.hWnd, _
                 TVM_SETBKCOLOR, _
                 0, _
                 ByVal RGB(255, 204, 0))
'Same as above except this one tells it to change
'the text colour
Call SendMessage(TreeView1.hWnd, _
                 TVM_SETTEXTCOLOR, _
                 0, _
                 ByVal RGB(0, 127, 0))
End Sub

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date