Results 1 to 14 of 14

Thread: TreeView

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    How do I change the background colour and foreground/text colour of a treeview?

    I'm a newbie at TV's, and I have no idea what to do!

    -Git

    [Edited by git on 08-12-2000 at 05:41 AM]

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Btw, how can you disable the keyboard in a TreeView, and how do you make a keyboard have an affect in a TreeView? Eg. when you click a node you use _Click, how do you do that so say if you use the arrows and enter/space or whatever it has an effect?

  3. #3
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Here's how you can change the color of TreeView. Add Common Dialog control and 2 buttons: cmdBackColor and cmdForeColor, then copy this code:
    Code:
    Option Explicit
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) 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
    
    'treeview styles
    Private Const TVS_HASLINES As Long = 2
    
    Private Const TV_FIRST As Long = &H1100
    Private Const TVM_SETBKCOLOR As Long = (TV_FIRST + 29)
    Private Const TVM_SETTEXTCOLOR As Long = (TV_FIRST + 30)
    
    Public Enum enumTreePartColor
        BACK_COLOR = 1
        FORE_COLOR = 2
    End Enum
    Public Sub SetTreeColor(pTreeView As TreeView, pTreePart As enumTreePartColor)
        Dim lngStyle As Long
        Dim intTreePart As Integer
        
        If pTreePart = BACK_COLOR Then
            intTreePart = TVM_SETBKCOLOR
        Else
            intTreePart = TVM_SETTEXTCOLOR
        End If
        
        
        With pTreeView
            Call SendMessage(.hwnd, intTreePart, 0, ByVal pColor)
        
            lngStyle = GetWindowLong(.hwnd, GWL_STYLE)
            If lngStyle And TVS_HASLINES Then
                Call SetWindowLong(.hwnd, GWL_STYLE, lngStyle Xor TVS_HASLINES)
                Call SetWindowLong(.hwnd, GWL_STYLE, lngStyle)
            End If
        End With
    End Sub
    
    Private Sub cmdBackColor_Click()
        Dim lngColor As Long
        
        On Error Resume Next
        
        With CommonDialog1
            .CancelError = True
            .ShowColor
            
            If Err.Number = cdlCancel Then Exit Sub
            
            lngColor = .Color
        End With
        
        SetTreeColor TreeView1, BACK_COLOR
    End Sub
    
    
    Private Sub cmdForeColor_Click()
        Dim lngColor As Long
        Dim lngStyle As Long
        
        On Error Resume Next
        
        With CommonDialog1
            .CancelError = True
            .ShowColor
            
            If Err.Number = cdlCancel Then Exit Sub
            
            lngColor = .Color
        End With
        SetTreeColor TreeView1, FORE_COLOR
    End Sub
    
    
    
    
    Private Sub Form_Load()
        Dim nodParent As Node
        Dim intParent As Integer
        Dim intChild As Integer
        
        With TreeView1.Nodes
        For intParent = 1 To 3
            Set nodParent = .Add(, , "Parent" & intParent, "Parent" & intParent)
            For intChild = 1 To 5
                .Add nodParent, tvwChild, , "ChildNode " & intChild
            Next
        Next
        End With
    End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    It doesn't work. On this line :

    Private Const TV_FIRST As Long = &H 1100

    It says Expected: Expression and highlights the =.

    Any idea what's wrong?

    Thanks,

    -Git

  5. #5
    Guest
    Vb-World's has bug: The 0's don't always come out correct, just put the 1100 by the H.

    Just fix it and everything should be okay.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    I finally got around to trying this out. There's one problem though, it says 'pColor' isn't defined.

    Any ideas? Thanks.

    Oh, and how would I pick a colour in the code (in RGB if possible), rather than getting a window pop up?

    -Git

  7. #7
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Oops, I forgot to include pColor as a parameter (this happens when I type directly into the post):
    Code:
    Option Explicit
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) 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
    
    'treeview styles
    Private Const TVS_HASLINES As Long = 2
    
    Private Const TV_FIRST As Long = &H1100
    Private Const TVM_SETBKCOLOR As Long = (TV_FIRST + 29)
    Private Const TVM_SETTEXTCOLOR As Long = (TV_FIRST + 30)
    
    Public Enum enumTreePartColor
        BACK_COLOR = 1
        FORE_COLOR = 2
    End Enum
    Public Sub SetTreeColor(pTreeView As TreeView, pTreePart As enumTreePartColor, pColor As OLE_COLOR)
        Dim lngStyle As Long
        Dim intTreePart As Integer
        
        If pTreePart = BACK_COLOR Then
            intTreePart = TVM_SETBKCOLOR
        Else
            intTreePart = TVM_SETTEXTCOLOR
        End If
        
        
        With pTreeView
            Call SendMessage(.hwnd, intTreePart, 0, ByVal pColor)
        
            lngStyle = GetWindowLong(.hwnd, GWL_STYLE)
            If lngStyle And TVS_HASLINES Then
                Call SetWindowLong(.hwnd, GWL_STYLE, lngStyle Xor TVS_HASLINES)
                Call SetWindowLong(.hwnd, GWL_STYLE, lngStyle)
            End If
        End With
    End Sub
    
    Private Sub cmdBackColor_Click()
        Dim lngColor As Long
        
        On Error Resume Next
        
        With CommonDialog1
            .CancelError = True
            .ShowColor
            
            If Err.Number = cdlCancel Then Exit Sub
            
            lngColor = .Color
        End With
        
        SetTreeColor TreeView1, BACK_COLOR, lngColor
    End Sub
    
    
    Private Sub cmdForeColor_Click()
        Dim lngColor As Long
        Dim lngStyle As Long
        
        On Error Resume Next
        
        With CommonDialog1
            .CancelError = True
            .ShowColor
            
            If Err.Number = cdlCancel Then Exit Sub
            
            lngColor = .Color
        End With
        SetTreeColor TreeView1, FORE_COLOR, lngColor
    End Sub
    
    
    
    
    Private Sub Form_Load()
        Dim nodParent As Node
        Dim intParent As Integer
        Dim intChild As Integer
        
        With TreeView1.Nodes
        For intParent = 1 To 3
            Set nodParent = .Add(, , "Parent" & intParent, "Parent" & intParent)
            For intChild = 1 To 5
                .Add nodParent, tvwChild, , "ChildNode " & intChild
            Next
        Next
        End With
    End Sub

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Now it says GWL_STYLE isn't defined ... =/

    Please help.

    Thanks!

    -Git

  9. #9
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Put this somewhere in the General Declarations area:
    Code:
    Private Const GWL_STYLE = (-16)
    You may get other non-defined things but come on, really...
    This is a TreeView! And it requires going to the Components window (OH THE HORROR)!
    So nobody really feels like actually trying out this code in VB.

    As for the no-popup-window and setting the color directly in the code, replace these subs:
    Code:
    Private Sub cmdBackColor_Click()
        Dim lngColor As Long
        
        On Error Resume Next
        
        With CommonDialog1
            .CancelError = True
            .ShowColor
            
            If Err.Number = cdlCancel Then Exit Sub
            
            lngColor = .Color
        End With
        
        SetTreeColor TreeView1, BACK_COLOR, lngColor
    End Sub
    
    
    Private Sub cmdForeColor_Click()
        Dim lngColor As Long
        Dim lngStyle As Long
        
        On Error Resume Next
        
        With CommonDialog1
            .CancelError = True
            .ShowColor
            
            If Err.Number = cdlCancel Then Exit Sub
            
            lngColor = .Color
        End With
        SetTreeColor TreeView1, FORE_COLOR, lngColor
    End Sub
    With these:
    Code:
    Private Sub cmdBackColor_Click()
        Dim lngColor As Long
    
        lngColor = RGB(50, 100, 200) ' BackColor
        
        SetTreeColor TreeView1, BACK_COLOR, lngColor
    End Sub
    
    Private Sub cmdForeColor_Click()
        Dim lngColor As Long
    
        lngColor = RGB(200, 100, 50) ' ForeColor
    
        SetTreeColor TreeView1, FORE_COLOR, lngColor
    End Sub

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Thank you!!! =D

    Works perfectly!

    Cya.

    -Git

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    What the.....?!

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Just goes to show you that in two years span, we're still asking the same questions
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I couldn't understand why my foreground color wouldn't change so I searched the web and found some similar code on VBNet (which also allows bolding) . When I ran that code it had the same problem. When I read the article that went along with that code it explained what the problem is. Here is what the article says.

    In developing the VB6 common controls, the VB development team stopped using comctl32.dll as its basis for functionality. Instead, the team took the unprecedented steps of removing the VB OCX dependency upon comctl32.dll, and opted (wrongly in this author's view) to rewrite the functionality available in comctl32.dll v4.72 as the new VB6 mscomctl.ocx. This redesigned OCX is not dependant on the original Windows' common control dll, and no longer relies on it for any functionality. And in creating the OCX, its implementation has been tweaked to the point where some methods that do work against the VB5 common control implementation (which uses comctl32.dll) no longer function as expected using the VB6 common control (mscomctl.ocx).

    The TreeView BackColor and ForeColor methods detailed here do not work the same under the VB6 mscomctl.dll-version of the common controls. Specifically, the ForeColor method fails to recolor all nodes; only the node losing focus exhibits the changed colour, momentarily, then reverts to the default WindowText colour. The BackgroundColor method will not colour the background area of the icon and text. FullRowSelect is available in the VB6-based control as a property; the API implementation shown here will not work correctly against a coloured background. Font bolding, thankfully, still works as indicated.


    So I'm still looking for code that does the foreground color of the VB6 control. Does anyone have any?

  14. #14
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You could probably extract some code from this project:

    http://www.planet-source-code.com/vb...25049&lngWId=1

    But I've always done ForeColor this way:

    VB Code:
    1. Private Sub Form_Load()
    2. Dim iNodes As Integer, nodX As Node
    3.  
    4.   For iNodes = 1 To 10
    5.     Set nodX = TreeView1.Nodes.Add(, , , "Node #" & iNodes)
    6.     nodX.ForeColor = vbBlue
    7.   Next
    8. End Sub
    My evil laugh has a squeak in it.

    kristopherwilson.com

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