Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

TreeView - set background color


'for setting treeview backcolor
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( ByVal hWnd _
    As Long , ByVal nIndex As Long ) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( ByVal hWnd _
    As Long , ByVal nIndex As Long , ByVal dwNewLong As Long ) As Long
Const GWL_STYLE = - 16&
Const TVM_SETBKCOLOR = 4381&
Const TVS_HASLINES = 2&


    Dim lStyle As Long
    Dim TVNode As Node
        
    ' set the BackColor for every node
    For Each TVNode In TV.Nodes
       TVNode.BackColor = BackColor
    Next

    ' set the BackColor for the TreeView's window
   SendMessage TV.hWnd, TVM_SETBKCOLOR, 0, ByVal BackColor
    ' get the current style
   lStyle = GetWindowLong(TV.hWnd, GWL_STYLE)
    ' temporary hide lines
   SetWindowLong TV.hWnd, GWL_STYLE, lStyle And ( Not TVS_HASLINES)
    ' redraw lines
   SetWindowLong TV.hWnd, GWL_STYLE, lStyle

'=============

Dim hwndTV As Long
   Dim Style As Long
  hwndTV = TV1.hWnd      'Change the background
   Call SendMessageLong(hwndTV, TVM_SETBKCOLOR, 0, clrref)
  'reset the treeview style so the
  'tree lines appear properly
  Style = GetWindowLong(TV1.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