Microsoft & .NETVisual BasicVB Coding Tip: Treeview - Changing Colours

VB Coding Tip: Treeview – Changing Colours content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories