换成位图就不大清楚,换色是可以的。例:
下面代码放入一公用模块:Option Explicit
Public Const GWL_STYLE As Long = (-16)
Public Const COLOR_WINDOW As Long = 5
Public Const COLOR_WINDOWTEXT As Long = 8Public Const TVI_ROOT   As Long = &HFFFF0000
Public Const TVI_FIRST  As Long = &HFFFF0001
Public Const TVI_LAST   As Long = &HFFFF0002
Public Const TVI_SORT   As Long = &HFFFF0003Public Const TVIF_STATE As Long = &H8'treeview styles
Public Const TVS_HASLINES As Long = 2
Public Const TVS_FULLROWSELECT As Long = &H1000'treeview style item states
Public Const TVIS_BOLD  As Long = &H10Public Const TV_FIRST As Long = &H1100
Public Const TVM_GETNEXTITEM As Long = (TV_FIRST + 10)
Public Const TVM_GETITEM As Long = (TV_FIRST + 12)
Public Const TVM_SETITEM As Long = (TV_FIRST + 13)
Public Const TVM_SETBKCOLOR As Long = (TV_FIRST + 29)
Public Const TVM_SETTEXTCOLOR As Long = (TV_FIRST + 30)
Public Const TVM_GETBKCOLOR As Long = (TV_FIRST + 31)
Public Const TVM_GETTEXTCOLOR As Long = (TV_FIRST + 32)Public Const TVGN_ROOT                As Long = &H0
Public Const TVGN_NEXT                As Long = &H1
Public Const TVGN_PREVIOUS            As Long = &H2
Public Const TVGN_PARENT              As Long = &H3
Public Const TVGN_CHILD               As Long = &H4
Public Const TVGN_FIRSTVISIBLE        As Long = &H5
Public Const TVGN_NEXTVISIBLE         As Long = &H6
Public Const TVGN_PREVIOUSVISIBLE     As Long = &H7
Public Const TVGN_DROPHILITE          As Long = &H8
Public Const TVGN_CARET               As Long = &H9Public Type TV_ITEM
   mask As Long
   hItem As Long
   state As Long
   stateMask As Long
   pszText As String
   cchTextMax As Long
   iImage As Long
   iSelectedImage As Long
   cChildren As Long
   lParam As Long
End TypePublic Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
   (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As LongPublic Declare Function GetWindowLong Lib "user32" _
   Alias "GetWindowLongA" _
   (ByVal hwnd As Long, _
    ByVal nIndex As Long) As LongPublic Declare Function SetWindowLong Lib "user32" _
   Alias "SetWindowLongA" _
   (ByVal hwnd As Long, _
    ByVal nIndex As Long, _
    ByVal dwNewLong As Long) As LongPublic Declare Function GetSysColor Lib "user32" _
   (ByVal nIndex As Long) As Long

解决方案 »

  1.   

    下面代码放入窗体,加上一TreeView与五个按钮与一个CommonDialog:
    Private Sub Form_Load()   Dim nodX As Node
       
      'add some test items
       Set nodX = TreeView1.Nodes.Add(, , "R", "Root")
       Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C1", "Child 1")
       Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C2", "Child 2")
       Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C3", "Child 3")
       Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C4", "Child 4")
       nodX.EnsureVisible
       
       Set nodX = TreeView1.Nodes.Add("C3", tvwChild, "C31", "Child 3 SubC 1")
       Set nodX = TreeView1.Nodes.Add("C3", tvwChild, "C32", "Child 3 SubC 2")
       nodX.EnsureVisible
       
       Set nodX = TreeView1.Nodes.Add("C31", tvwChild, "C321", "Child 3 SubC 1 SubC 1")
       
       Set nodX = TreeView1.Nodes.Add("C4", tvwChild, "C41", "Child 4 Subchild 1")
       nodX.EnsureVisible
       
    End Sub
    Private Sub cmdEnd_Click()   Unload Me
       
    End Sub
    Private Function GetTVBackColour() As Long   Dim clrref As Long
       Dim hwndTV As Long
       
       hwndTV = TreeView1.hwnd
       
      'try for the treeview backcolor
       clrref = SendMessage(hwndTV, TVM_GETBKCOLOR, 0, ByVal 0)
       
      'if clrref = -1, then the color is a system color.
      'In theory, system colors need to be Or'd with &HFFFFFF
      'to retrieve the actual RGB value, but not Or'ing
      'seems to work for me. The default system colour for
      'a treeview background is COLOR_WINDOW.
       If clrref = -1 Then
          clrref = GetSysColor(COLOR_WINDOW)  ' Or &HFFFFFF
       End If
       
      'one way or another, pass it back
       GetTVBackColour = clrref
       
    End Function
    Private Function GetTVForeColour() As Long   Dim clrref As Long
       Dim hwndTV As Long
       
       hwndTV = TreeView1.hwnd
       
      'try for the treeview text colour
       clrref = SendMessage(hwndTV, TVM_GETTEXTCOLOR, 0, ByVal 0)
       
      'if clrref = -1, then the color is a system color.
      'In theory, system colors need to be Or'd with &HFFFFFF
      'to retrieve the actual RGB value, but not Or'ing
      'seems to work for me. The default system colour for
      'treeview text is COLOR_WINDOWTEXT.
       If clrref = -1 Then
          clrref = GetSysColor(COLOR_WINDOWTEXT) ' Or &HFFFFFF
       End If
       
      'one way or another, pass it back
       GetTVForeColour = clrref
       
    End Function
    Private Sub SetTVBackColour(clrref As Long)   Dim hwndTV As Long
       Dim style As Long
       
       hwndTV = TreeView1.hwnd
       
      'Change the background
       Call SendMessage(hwndTV, TVM_SETBKCOLOR, 0, ByVal clrref)
       
      'reset the treeview style so the
      'tree lines appear properly
       style = GetWindowLong(TreeView1.hwnd, GWL_STYLE)
       
      'if the treeview has lines, temporarily
      'remove them so the back repaints to the
      'selected colour, then restore
       If style And TVS_HASLINES Then
          Call SetWindowLong(hwndTV, GWL_STYLE, style Xor TVS_HASLINES)
          Call SetWindowLong(hwndTV, GWL_STYLE, style)
       End If
      
    End Sub
    Private Sub SetTVForeColour(clrref As Long)   Dim hwndTV As Long
       Dim style As Long
       
       hwndTV = TreeView1.hwnd
       
      'Change the background
       Call SendMessage(hwndTV, TVM_SETTEXTCOLOR, 0, ByVal clrref)
       
      'reset the treeview style so the
      'tree lines appear properly
       style = GetWindowLong(TreeView1.hwnd, GWL_STYLE)
       
      'if the treeview has lines, temporarily
      'remove them so the back repaints to the
      'selected colour, then restore
       If style And TVS_HASLINES Then
          Call SetWindowLong(hwndTV, GWL_STYLE, style Xor TVS_HASLINES)
          Call SetWindowLong(hwndTV, GWL_STYLE, style)
       End If
       
    End Sub
    Private Sub cmdSetBackground_Click()   Dim newclr As Long
       
       With cDlg
          .Flags = cdlCCRGBInit      'using RGB colours
          .Color = GetTVBackColour() 'pre-select the current colour
          .ShowColor                 'get the user's choice
          newclr = .Color            'and assign to a var
       End With
       
       SetTVBackColour newclr        'set the backcolour
       
    End Sub
    Private Sub cmdBold_Click()   Dim TVI As TV_ITEM
       Dim hitemTV As Long
       Dim hwndTV As Long
       
      'get the handle to the treeview item.
      'If the item is selected, use TVGN_CARET.
      'To highlight the first item in the root, use TVGN_ROOT
      'To hilight the first visible, use TVGN_FIRSTVISIBLE
      'To hilight the selected item, use TVGN_CARET
       hwndTV = TreeView1.hwnd
       hitemTV = SendMessage(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, ByVal 0&)
       
      'if a valid handle get and set the
      'item's state attributes
       If hitemTV > 0 Then
       
          With TVI
             .hItem = hitemTV
             .mask = TVIF_STATE
             .stateMask = TVIS_BOLD
              Call SendMessage(hwndTV, TVM_GETITEM, 0&, TVI)
             
             'flip the bold mask state
             .state = TVIS_BOLD
          End With
          
          Call SendMessage(hwndTV, TVM_SETITEM, 0&, TVI)
     
       End If
       
    End Sub
    Private Sub cmdFullRow_Click()   Dim hwndTV As Long
       Dim style As Long  'get the window style
       style = GetWindowLong(TreeView1.hwnd, GWL_STYLE)
       
      'toggle the fullrow select
       If style And TVS_FULLROWSELECT Then
             style = style Xor TVS_FULLROWSELECT
       Else: style = style Or TVS_FULLROWSELECT
       End If
       
      'and set it
       Call SetWindowLong(TreeView1.hwnd, GWL_STYLE, style)
       
    End Sub
    Private Sub cmdSetText_Click()   Dim newclr As Long
       
       With cDlg
          .Flags = cdlCCRGBInit      'using RGB colours
          .Color = GetTVForeColour() 'pre-select the current colour
          .ShowColor                 'get the user's choice
          newclr = .Color            'and assign to a var
       End With
       
       SetTVForeColour newclr        'set the text colour
       
    End Sub