In the third part of this tutorial, we continue to learn more about the treeview control. Past parts include:
- The basics
- Changing colours
This part allows us to add features such as track selection and checkboxes. It is quite simple, all you need is an API call and a few constants.
This example includes the last two parts code:<\p>
'API Call for Sending the messages 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 '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 Const TVS_CHECKBOXES = &H100 Private Const TVS_TRACKSELECT = &H200 Private Sub Form_Load() 'This is part 1's 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 part 2's 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)) 'This is part 3's code 'Add the track selection Call SetTreeViewAttrib(TreeView1, TVS_TRACKSELECT) 'Add the checkboxes Call SetTreeViewAttrib(TreeView1, TVS_CHECKBOXES) End Sub Private Sub SetTreeViewAttrib(c As TreeView, _ ByVal Attrib As Long) Const GWL_STYLE As Long = -16 Dim rStyle As Long rStyle = GetWindowLong(c.hWnd, GWL_STYLE) rStyle = rStyle Or Attrib Call SetWindowLong(c.hWnd, GWL_STYLE, rStyle) End Sub