本文中给出了VB中改变TreeView的背景颜色的方法,主要使用了API函数。
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
私有声明函数 GetWindowLong Lib User32 别名 GetWindowLongA (ByVal hWnd As Long, ByVal nIndex As Long) As Long
私有声明函数 SetWindowLong Lib User32 别名 SetWindowLongA (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
私有常量 GWL_STYLE = -16&
私有常量 TVM_SETBKCOLOR = 4381&
私有常量 TVM_GETBKCOLOR = 4383&
私有常量 TVS_HASLINES = 2&
Dim frmlastForm 作为表单
私有子表单_Load()
Dim nodX 作为节点
设置 nodX = TreeView1.Nodes.Add(, , R, Root)
设置 nodX = TreeView1.Nodes.Add(R, tvwChild, C1, Child 1)
设置 nodX = TreeView1.Nodes.Add(R, tvwChild, C2, Child 2)
设置 nodX = TreeView1.Nodes.Add(R, tvwChild, C3, Child 3)
设置 nodX = TreeView1.Nodes.Add(R, tvwChild, C4, Child 4)
nodX.EnsureVisible
TreeView1.style = tvwTreelinesText ' 样式 4。
TreeView1.BorderStyle = vbFixedSingle
结束子
私有子Command1_Click()
调暗样式只要长
调用 SendMessage(TreeView1.hWnd, TVM_SETBKCOLOR, 0, ByVal RGB(255, 0, 0))
'改变背景到红色
lngStyle = GetWindowLong(TreeView1.hWnd, GWL_STYLE)
调用 SetWindowLong(TreeView1.hWnd, GWL_STYLE, lngStyle - TVS_HASLINES)
调用 SetWindowLong(TreeView1.hWnd, GWL_STYLE, lngStyle)
结束子
选项显式
私有声明函数 SendMessage Lib User32 别名 SendMessageA (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
私有声明函数 GetWindowLong Lib User32 别名 GetWindowLongA (ByVal hWnd As Long, ByVal nIndex As Long) As Long
私有声明函数 SetWindowLong Lib User32 别名 SetWindowLongA (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
私有常量 GWL_STYLE = -16&
私有常量 TVM_SETBKCOLOR = 4381&
私有常量 TVM_GETBKCOLOR = 4383&
私有常量 TVS_HASLINES = 2&
私有子表单_Load()
Dim nodex 作为节点
将 i 调暗为整数
暗淡风格只要长
调用 SendMessage(TreeView1.hWnd, TVM_SETBKCOLOR, 0, ByVal RGB(255, 0, 0))
lStyle = GetWindowLong(TreeView1.hWnd, GWL_STYLE)
调用 SetWindowLong(TreeView1.hWnd, GWL_STYLE, lStyle - TVS_HASLINES)
调用 SetWindowLong(TreeView1.hWnd, GWL_STYLE, lStyle)
TreeView1.Style = tvwTreelinesPlusMinusPictureText
TreeView1.BorderStyle = ccFixedSingle
使用 TreeView1.Nodes
设置nodex = .Add(, , R, Root)
nodex.BackColor = RGB(255, 0, 0)
对于 i = 1 到 10
设置nodex = .Add(R, tvwChild, C & i, Child & i)
nodex.BackColor = RGB(255, 0, 0)
nodex.EnsureVisible
下一个
结束于
结束子