本文给出在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
Função de declaração privada GetWindowLong Lib User32 Alias GetWindowLongA (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Função de declaração privada SetWindowLong Lib User32 Alias SetWindowLongA (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const Privado GWL_STYLE = -16&
Privado Const TVM_SETBKCOLOR = 4381&
Privado Const TVM_GETBKCOLOR = 4383&
Const privado TVS_HASLINES = 2&
Dim frmlastForm As Form
Subformulário privado_Load()
Dim nodX como nó
Definir nodX = TreeView1.Nodes.Add(,, R, Root)
Definir nodX = TreeView1.Nodes.Add(R, tvwChild, C1, Filho 1)
Definir nodX = TreeView1.Nodes.Add(R, tvwChild, C2, Filho 2)
Definir nodX = TreeView1.Nodes.Add(R, tvwChild, C3, Filho 3)
Definir nodX = TreeView1.Nodes.Add(R, tvwChild, C4, Filho 4)
nodX.EnsureVisible
TreeView1.style = tvwTreelinesText ' Estilo 4.
TreeView1.BorderStyle = vbFixedSingle
Finalizar sub
Subcomando Privado1_Click()
Dim lngStyle enquanto
Chame SendMessage(TreeView1.hWnd, TVM_SETBKCOLOR, 0, ByVal RGB(255, 0, 0))
'改变背景到红色
lngStyle = GetWindowLong(TreeView1.hWnd, GWL_STYLE)
Chame SetWindowLong (TreeView1.hWnd, GWL_STYLE, lngStyle - TVS_HASLINES)
Chame SetWindowLong(TreeView1.hWnd, GWL_STYLE, lngStyle)
Finalizar sub
Opção Explícita
Função de declaração privada SendMessage Lib User32 Alias SendMessageA (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Função de declaração privada GetWindowLong Lib User32 Alias GetWindowLongA (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Função de declaração privada SetWindowLong Lib User32 Alias SetWindowLongA (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const Privado GWL_STYLE = -16&
Privado Const TVM_SETBKCOLOR = 4381&
Privado Const TVM_GETBKCOLOR = 4383&
Const privado TVS_HASLINES = 2&
Subformulário privado_Load()
Dim nodex como nó
Dim i como inteiro
Dim lStyle tão longo
Chame SendMessage(TreeView1.hWnd, TVM_SETBKCOLOR, 0, ByVal RGB(255, 0, 0))
lStyle = GetWindowLong(TreeView1.hWnd, GWL_STYLE)
Chame SetWindowLong (TreeView1.hWnd, GWL_STYLE, lStyle - TVS_HASLINES)
Chame SetWindowLong(TreeView1.hWnd, GWL_STYLE, lStyle)
TreeView1.Style = tvwTreelinesPlusMinusPictureText
TreeView1.BorderStyle = ccFixedSingle
Com TreeView1.Nodes
Definir nodex = .Add(, , R, Root)
nodex.BackColor = RGB(255, 0, 0)
Para i = 1 a 10
Definir nodex = .Add(R, tvwChild, C & i, Child & i)
nodex.BackColor = RGB(255, 0, 0)
nodex.EnsureVisible
Próximo
Terminar com
Finalizar sub