No announcement yet.

Toggle Style off for a TreeView control

  • Filter
  • Time
  • Show
Clear All
new posts

  • Toggle Style off for a TreeView control

    Hi All!

    I have used this method in the past to turn off certain window styles and
    it has worked great for me. But for some reason I can't seem to
    turn off the Horizontal and Vertical ScrollBars on a TreeView window.

    Here is how I created the a very simple TreeView, note I did not specifiy
    that %WS_HSCROLL or %WS_VSCROLL styles when I created it.

    FUNCTION CreateNewTreeView(ByVal hWnd AS LONG, rc AS RECT) AS LONG
        LOCAL hWndNew AS LONG
        LOCAL tvs          AS TV_INSERTSTRUCT
        LOCAL tvi          AS TV_ITEM
        LOCAL hTreeWnd()   AS LONG
        LOCAL sText        AS STRING
            'Create the TreeView control
            hWndNew = CreateWindowEx(%WS_EX_CLIENTEDGE ,"SysTreeView32","", _
                                     %WS_CHILD OR %WS_VISIBLE OR %WS_CLIPSIBLINGS _
                                     OR %TVS_HASBUTTONS OR %TVS_HASLINES _
                                     OR %TVS_LINESATROOT OR %TVS_SHOWSELALWAYS, _
                                     rc.nLeft, _
                                     rc.nTop,  _
                                     rc.nRight  - rc.nLeft, _
                                     rc.nBottom - rc.nTop,  _
                                     hWnd,ByVal %NULL,ghInst,ByVal %Null)
            'Initialize the tree List
            tvs.hInsertAfter = %TVI_LAST
            tvi.mask         = %TVIF_TEXT
            REDIM hTreeWnd(0 To 4)
            sText = "One"
            tvi.pszText      = StrPtr(sText)
            tvs.hParent      = %TVI_ROOT
            tvs.item         = tvi
            hTreeWnd(0)      = TreeView_InsertItem(hWndNew,tvs)
            sText = "Two"
            tvi.pszText      = StrPtr(sText)
            tvs.hParent      = hTreeWnd(0)
            tvs.item         = tvi
            hTreeWnd(1)      = TreeView_InsertItem(hWndNew,tvs)
            sText = "Buckle"
            tvi.pszText      = StrPtr(sText)
            tvs.hParent      = hTreeWnd(1)
            tvs.item         = tvi
            hTreeWnd(2)      = TreeView_InsertItem(hWndNew,tvs)
            sText = "My"
            tvi.pszText      = StrPtr(sText)
            tvs.hParent      = hTreeWnd(2)
            tvs.item         = tvi
            hTreeWnd(3)      = TreeView_InsertItem(hWndNew,tvs)
            sText = "Shoe"
            tvi.pszText      = StrPtr(sText)
            tvs.hParent      = hTreeWnd(2)
            tvs.item         = tvi
            hTreeWnd(4)      = TreeView_InsertItem(hWndNew,tvs)
            'Expand All
            For i& = 0 to 4
                Call TreeView_Expand( hWndNew, hTreeWnd(i&), %TVE_EXPAND )
            'SubClass the control 
            Call SetWindowLong(hWndNew,%GWL_WNDPROC,ByVal CodePtr(ControlSubClassProc))
            'Get rid of the scroll bars temporarly
            Oldstyle& = GetWindowLong( hWndNew,%GWL_STYLE )
            Newstyle& = Oldstyle& XOR %WS_VSCROLL XOR %WS_HSCROLL
            Call SetWindowLong( hWndNew, %GWL_STYLE , Newstyle&)
        FUNCTION = hWndNew
    When the control is created with the tree fully expanded but not in view, the
    scroll bars automatically come on. I would like to temporarly turn
    this off. Is there a TVS_AUTOHSCROLL/TVS_AUTOVSCROLL style not documented?

    How would I do this?

    Thanks for any suggestions/help!

    Regards, Jules

  • #2
    If this code is for your visual designer then let me offer a suggestion.
    Do not use SetWindowLong to set and remove the styles of controls. SetWindowLong is
    very limited in what it can change on the fly. The knowledge base article
    "Value Returned by GetWindowLong(hWnd, GWL_STYLE)" (ID: Q83366) sheds some light on this.

    Destroy and recreate the control using the current styles.

    The treeview control is a case in point(on my system).
    Depending on the version of insanity(comctl32.dll or internet explorer) that is install
    on your system you can use the TVS_NOSCROLL(0x2000) style to disable scrolling.
    However, if the treeview control has the scroll bars visible SetWindowLong with TVS_NOSCROLL
    disables scrolling but the scrollbars remain visible. If the control is created with
    TVS_NOSCROLL set initially then SetWinowLong with TVS_NOSCROLL cleared enables scrolling and
    the scrollbars become visible when needed. You do not encounter such madness when you destroy
    and recreate the control.

    I am curious as to why you would want to hide the scroll bars?

    Dominic Mitchell

    [This message has been edited by Dominic Mitchell (edited September 09, 2000).]
    Dominic Mitchell
    Phoenix Visual Designer


    • #3

      Thank you for this information. This very useful stuff. I am not at liberty
      to discuss the reason online at this time. I can however, email you my code
      (~50kbytes snippet) to show you exactly the reason. I have found a fix for
      my problem to the scroll bar issue rather then changing the sytle on the fly.

      My email is [email protected] if you are interested.

      Thanks again!
      Regards, Jules