Changing backcolor and forecolor of TreeView control 6.0 (SP6)

Posted on 2010-01-05
Last Modified: 2013-11-28
Hi experts,

I have an ActiveX Microsoft TreeView6.0(SP6) control on my form.  The ActiveX control is working well, but I would like to change the background color and forecolor to match the rest of the forms background color.  

Can anybody help?

Thank you.
Question by:TerenceHewett
    LVL 46

    Accepted Solution

    Have a look at the following:


    Author Comment

    Thanks for your help Gary.  This info was very useful.

    LVL 46

    Expert Comment

    Glad to help:
    Here's the 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 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
          Private Const GWL_STYLE = -16&
          Private Const TVM_SETBKCOLOR = 4381&
          Private Const TVM_GETBKCOLOR = 4383&
          Private Const TVS_HASLINES = 2&
          Dim frmlastForm As Form
          Private Sub Form_Load()
              Dim nodX As Node
              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")
              TreeView1.Style = tvwTreelinesText ' Style 4.
              TreeView1.BorderStyle = vbFixedSingle
          End Sub
          Private Sub Command1_Click()
             Dim lngStyle As Long
             Call SendMessage(TreeView1.hWnd, _
                               TVM_SETBKCOLOR, _
                               0, _
                               ByVal RGB(255, 0, 0))  'Change the background
                                                      'color to red.
          ' Now reset the style so that the tree lines appear properly
             lngStyle = GetWindowLong(TreeView1.hWnd, GWL_STYLE)
             Call SetWindowLong(TreeView1.hWnd, _
                               GWL_STYLE, _
                               lngStyle - TVS_HASLINES)
             Call SetWindowLong(TreeView1.hWnd, GWL_STYLE, lngStyle)
          End Sub

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now