Microsoft KB Archive/299686

= How To Detect If Scroll Bars Are Visible on a Control =

Article ID: 299686

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q299686



SUMMARY
Controls such as RichTextBox, TreeView, ListView, ListBox, DirListBox, and FileListBox do not offer built-in functionality to determine if a horizontal or vertical scroll bar is currently visible. This article demonstrates how to retrieve window style information about the control that can be used to determine if scroll bars are visible.



MORE INFORMATION
The visibility of a scroll bar is stored as a window style of the control. To retrieve the window style information from a control, you can call the GetWindowLong function.

Step-by-Step Example
 Start a new Standard EXE project in Visual Basic. Form1 is created by default. On the Project menu, click Components. In the Components dialog box, select the Microsoft Windows Common Controls 6.0 check box, and then click OK. Add one TreeView and two CommandButton controls to Form1.  Add the following code to the General Declarations section of Form1: Option Explicit

Private Const GWL_STYLE = (-16) Private Const WS_HSCROLL = &H100000 Private Const WS_VSCROLL = &H200000

Private Declare Function GetWindowLong Lib &quot;user32&quot; Alias &quot;GetWindowLongA&quot; & _ (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Private Sub Command1_Click Dim wndStyle As Long ' Retrieve the window style of the control. wndStyle = GetWindowLong(TreeView1.hwnd, GWL_STYLE)

' Test if the horizontal scroll bar style is present ' in the window style, indicating that a horizontal ' scroll bar is visible. If (wndStyle And WS_HSCROLL) <> 0 Then MsgBox &quot;A horizontal scroll bar is visible.&quot; Else MsgBox &quot;A horizontal scroll bar is NOT visible.&quot; End If   ' Test if the vertical scroll bar style is present ' in the window style, indicating that a vertical ' scroll bar is visible. If (wndStyle And WS_VSCROLL) <> 0 Then MsgBox &quot;A vertical scroll bar is visible.&quot; Else MsgBox &quot;A vertical scroll bar is NOT visible.&quot; End If End Sub

Private Sub Command2_Click ' Make the size of the control smaller ' so that the scroll bars will appear. TreeView1.Move 250, 900, 1000, 1000 End Sub

Private Sub Form_Load ' Size and position the form and controls. Form1.ScaleMode = 1 Form1.Move 0, 0, 5100, 5040 Command1.Caption = &quot;Scroll Bar Test&quot; Command1.Move 120, 120, 1700, 500 Command2.Caption = &quot;Size Control&quot; Command2.Move 2000, 120, 1700, 500 TreeView1.Move 250, 900, 3000, 1500 ' Add sample text to the TreeView control. TreeView1.Nodes.Add, , , &quot;1: Sample Text&quot; TreeView1.Nodes.Add, , , &quot;2: Sample Text&quot; TreeView1.Nodes.Add, , , &quot;3: Sample Text&quot; TreeView1.Nodes.Add, , , &quot;4: Sample Text&quot; End Sub  Run the project. Notice that no scroll bars are present on the TreeView control. Click Scroll Bar Test. A message box indicates that a horizontal scroll bar is not present. Another message box indicates that a vertical scroll bar is not present. Click Size Control. Notice that the height and width of the TreeView control are smaller and that both a horizontal and vertical scroll bar are visible.</li> Click Scroll Bar Test. A message box indicates that a horizontal scroll bar is present, which is followed by another message box that indicates that a vertical scroll bar is present.</li></ol>

<div class="references_section">