本文給出了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
下一個
結束於
結束子