PDA

Click to See Complete Forum and Search --> : change backcolor of treeview control


ruvik
Nov 20th, 2000, 02:39 PM
How can I change the back color of a treeview control in VB 6. It does not have the property 'backcolor' so i really have no idea.
Can anyone help me? thank you.

juliomont
Nov 20th, 2000, 07:48 PM
HOWTO: Change the TreeView Control Background Color

The information in this article applies to:
· Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0


SUMMARY
This article illustrates how to change the background color of the TreeView control to fit your user interface requirements.

MORE INFORMATION
The Custom Controls Library file, comctl32.dll, version 4.70 or later, is required to change the TreeView background color. This file is available by installing Internet Explorer version 4.0 or later. Note that this file is not currently redistributable. If you need this file, then you must require your users to install Internet Explorer 4.0. Please review the information regarding the use and redistribution rights for Microsoft Internet Explorer located at:
http://www.microsoft.com/ie/

-and-

http://www.microsoft.com/ie/ieak/

Should you have questions regarding the licensing of Internet Explorer, please contact ieak@microsoft.com.

To change the background color of a TreeView control, you can call the following 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 demonstrates how to use these functions to change the background color. The section assumes you have the correct version of comctl32.dll installed on your computer.

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 unsatisfactory results. This is because the TreeView and most other Windows controls cannot display text over a dithered background. Change the last parameter of 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:
8. Option Explicit
9.
10. Private Declare Function SendMessage Lib "User32" _
11. Alias "SendMessageA" _
12. (ByVal hWnd As Long, _
13. ByVal wMsg As Long, _
14. ByVal wParam As Long, _
15. lParam As Long) As Long
16.
17. Private Declare Function GetWindowLong Lib "User32" _
18. Alias "GetWindowLongA" _
19. (ByVal hWnd As Long, _
20. ByVal nIndex As Long) As Long
21.
22. Private Declare Function SetWindowLong Lib "User32" _
23. Alias "SetWindowLongA" _
24. (ByVal hWnd As Long, _
25. ByVal nIndex As Long, _
26. ByVal dwNewLong As Long) As Long
27.
28. Private Const GWL_STYLE = -16&
29. Private Const TVM_SETBKCOLOR = 4381&
30. Private Const TVM_GETBKCOLOR = 4383&
31. Private Const TVS_HASLINES = 2&
32.
33. Dim frmlastForm As Form
34.
35. Private Sub Form_Load()
36. Dim nodX As Node
37. Set nodX = TreeView1.Nodes.Add(, , "R", "Root")
38. Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C1", "Child 1")
39. Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C2", "Child 2")
40. Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C3", "Child 3")
41. Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C4", "Child 4")
42. nodX.EnsureVisible
43. TreeView1.style = tvwTreelinesText ' Style 4.
44. TreeView1.BorderStyle = vbFixedSingle
45. End Sub
46.
47. Private Sub Command1_Click()
48. Dim lngStyle As Long
49.
50. Call SendMessage(TreeView1.hWnd, _
51. TVM_SETBKCOLOR, _
52. 0, _
53. ByVal RGB(255, 0, 0)) 'Change the background
54. 'color to red.
55.
56. ' Now reset the style so that the tree lines appear properly
57. lngStyle = GetWindowLong(TreeView1.hWnd, GWL_STYLE)
58. Call SetWindowLong(TreeView1.hWnd, _
59. GWL_STYLE, _
60. lngStyle - TVS_HASLINES)
61. Call SetWindowLong(TreeView1.hWnd, GWL_STYLE, lngStyle)
62. End Sub

63. On the Run menu, click Start or press the F5 key to start the program.
64. Click the CommandButton. The TreeView background color changes to red.

REFERENCES
Platform SDK Documentation on the following functions:
· SendMessage
· GetWindowLong
· SetWindowLong
Additional query words:
Keywords : kbVBp kbVBp500 kbVBp600
Issue type : kbhowto
Technology :