Results 1 to 6 of 6

Thread: Change The Background of The Treeview Control

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    NY, USA.
    Posts
    240

    Question

    Hi,
    Does anyone know how to change the background of a Treeview control

    Example :
    Placing a picture on the background or Just change the background color.
    Omar
    [email protected]
    http://omar.caribwalk.com
    To God Be The Glory

    I see Tech People ...

  2. #2
    Guest
    unfortunatly the only way to change the backcolor of a treeview is subclassing (and quite a bit of work). sheridan (www.shersoft.com) offers a SSTreeView control with the possibility to change backcolor. you can download a demo version from there, I think buying that one is cheaper then writing it yourself (maybe less bugs too?).

    best regards

    Sascha

  3. #3
    Guest
    add this to a module

    Code:
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    
    
    Public Enum TVMColorProps
    TVM_GETBKCOLOR = 4383
    TVM_GETTEXTCOLOR = 4384
    TVM_SETBKCOLOR = 4381
    TVM_SETTEXTCOLOR = 4382
    End Enum
    
    Public Const GWL_STYLE As Long = -16
    Public Const TVIS_BOLD As Long = 16
    Public Const TVIF_STATE As Long = 8
    Public Const TVS_HASLINES As Long = 2
    
    
    Public Function SetTVBackgroundColor(ColorValue As Long, TreeViewControl As TreeView) As Long
    On Error GoTo error_SetTVBackgroundColor
    Dim lngLineStyle As Long
    Dim lngRet As Long
    
    With TreeViewControl
    lngRet = SendMessage(.hwnd, TVM_SETBKCOLOR, 0, ByVal ColorValue)
    lngLineStyle = GetWindowLong(.hwnd, GWL_STYLE)
    If (lngLineStyle And TVS_HASLINES) = True Then
    lngRet = SetWindowLong(.hwnd, GWL_STYLE, lngLineStyle Xor tvshaslines)
    lngRet = SetWindowLong(.hwnd, GWL_STYLE, lngLineStyle)
    End If
    
    SetTVBackgroundColor = 1
    End With
    Exit Function
    error_SetTVBackgroundColor:
    SetTVBackgroundColor = 0
    
    End Function
    add this to the form load or wheneever you want to set the BG color

    Code:
    SetTVBackgroundColor RGB(0, 200, 255), TreeView1
    change the rgb values to your liking, TreeView1 is the name of the treeview control...

    [Edited by denniswrenn on 05-28-2000 at 12:15 PM]

  4. #4
    Guest
    if you use the api, like dennis did (there is a typing error, replace tvshaslines by TVS_HASLINES), you have to change each node's backcolor too (easy one).

    good luck

    Sascha

  5. #5
    Guest
    oopsie.....
    it worked for me...
    I have no idea how to use treeview control, and i have never had a need to use it, so I didnt know about changing the nodes too....

  6. #6
    Guest
    paste the following code into a new UserControl and add a TreeView with name 'Tree'. this should do part of the trick.

    '**********************************************************

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hWnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, ByVal hParam As Long) As Long

    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

    Public Enum asTreeViewItemMove
    asTreeViewRoot = &H0
    asTreeViewNext = &H1
    asTreeViewPrevious = &H2
    asTreeViewParent = &H3
    asTreeViewChild = &H4
    asTreeViewFirstVisible = &H5
    asTreeViewNextVisible = &H6
    asTreeViewPreviousVisible = &H7
    asTreeViewDropHilite = &H8
    asTreeViewCaret = &H9
    End Enum

    Public Enum asTreeViewItemProps
    asTreeViewGetNextItem = 4362
    asTreeViewGetItem = 4364
    asTreeViewSetItem = 4365
    End Enum

    Public Enum asTreeViewColorProps
    asTreeViewSetBackColor = 4381
    asTreeViewSetTextColor = 4382
    asTreeViewGetBackColor = 4383
    asTreeViewGetTextColor = 4384
    End Enum

    Public Enum asTreeViewStyleConstants
    asTreeViewGwlStyle = -16
    asTreeViewIsBold = 16
    asTreeViewIfState = 8
    asTreeViewHasLines = 2
    End Enum

    Private mlngBackColor As OLE_COLOR
    Private mlngForeColor As OLE_COLOR

    Private Sub UserControl_Initialize()

    Dim n As Node, i As Integer, j As Integer

    With Tree
    Set n = .Nodes.Add(, , "\", "MyComputer")
    For i = 1 To 10
    Set n = .Nodes.Add("\", tvwChild, "\Child" & i, "Child" & i)
    For j = 1 To 5
    Set n = .Nodes.Add("\Child" & i, tvwChild, "\Child" & i & "\Sub" & j, "SubChild" & j)
    Next
    Next
    End With

    ForeColor = vbWhite
    BackColor = &H400000

    End Sub

    Public Property Let BackColor(ByVal Value As OLE_COLOR)

    mlngBackColor = Value
    SetTreeBackColor

    End Property

    Public Property Get BackColor() As OLE_COLOR

    BackColor = mlngBackColor

    End Property

    Public Property Let ForeColor(ByVal Value As OLE_COLOR)

    Dim n As Node

    mlngForeColor = Value

    For Each n In Tree.Nodes
    n.ForeColor = mlngForeColor
    Next

    End Property

    Public Property Get ForeColor() As OLE_COLOR

    ForeColor = mlngForeColor

    End Property

    Private Sub SetTreeBackColor()

    Dim n As Node, lngReturn As Long, lngLineStyle As Long

    With Tree
    lngReturn = SendMessage(.hWnd, asTreeViewSetBackColor, 0, ByVal mlngBackColor)
    lngLineStyle = GetWindowLong(.hWnd, asTreeViewGwlStyle)
    If (lngLineStyle And asTreeViewHasLines) = True Then
    lngReturn = SetWindowLong(.hWnd, asTreeViewGwlStyle, lngLineStyle Xor asTreeViewHasLines)
    lngReturn = SetWindowLong(.hWnd, asTreeViewGwlStyle, lngLineStyle)
    End If
    End With

    For Each n In Tree.Nodes
    n.BackColor = mlngBackColor
    Next

    End Sub

    '**********************************************************

    best regards

    Sascha

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width