February 28, 1996
This article explains how to determine whether a window or control has a scroll bar associated with it when your Microsoft® Visual Basic® 4.0 application is running.
When designing a Microsoft® Visual Basic® version 4.0 application, you may need to determine at run time whether a specific control has a scroll bar. This can be accomplished by using the Microsoft® Windows® application programming interface (API) GetWindowLong function.
Every form (or window) in a Microsoft Windows-based application has a set of attributes associated with it. These window style bits describe the window. For example, if a window has a border, the WS_BORDER style bit is set. In the same manner, if a window has a scroll bar, the WS_VSCROLL and/or WS_HSCROLL style bits are set.
You can call the GetWindowLong function to determine whether a control has a scroll bar. The GetWindowLong function requires two arguments: the handle of the window you want to retrieve information for, and the type of information you want to retrieve. In this case, you tell the GetWindowLong function to retrieve the style of the window (GWL_STYLE).
After the GetWindowLong function is called, you use the Visual Basic AND operator to find out whether the style of the window is set to that of a vertical scroll bar (&H200000). If this statement indicates that a scroll bar is present, the expression returns a True value.
This program shows how to determine whether a control has a scroll bar.
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Const GWL_STYLE = (-16)
Private Sub Command1_Click()
Dim lFlag As Long, iGotScroll As Long
lFlag = GetWindowLong(File1.hwnd, GWL_STYLE)
If (&H200000 And lFlag) <> False Then
iGotScroll = True
MsgBox "File List Box has a scroll bar"
Else
iGotScroll = False
MsgBox "File List Box does NOT have a scroll bar"
End If
lFlag = GetWindowLong(Command1.hwnd, GWL_STYLE)
If (&H200000 And lFlag) <> False Then
iGotScroll = True
MsgBox "Command Button has a scroll bar"
Else
iGotScroll = False
MsgBox "Command Button does NOT have a scroll bar"
End If
End Sub
Run the example program by pressing F5. Click the Command Button control. A message box appears indicating that the File List Box control does have a scroll bar. Click OK. A message box appears indicating that the Command Button control does not have a scroll bar. Click OK.
"GetWindowLong QuickInfo Overview Group." (MSDN Library, Platform, SDK, and DDK Documentation)
Knowledge Base Q109741. "How to Scroll a Form When VB Forms Are Limited to Screen Size."
"Scrolling Windows." (MSDN Library, Specifications)
"Tip 138: Enabling and Disabling Scroll Bars in a List Box."