Sometimes, it’s useful to know whether a control is displaying vertical scrollbars, horizontal scrollbars, both horizontal and vertical, or no scrollbars at all. For instance, when determining the amount of room inside a control, you might have to take into account the space taken up by the scrollbars. This function, written in VB5, returns an enumerated constant representing the scrollbar state of the given control:
Private Declare Function GetWindowLong Lib "user32" Alias _ "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex _ As Long) As Long'GetWindowLong ConstantsPrivate Const GWL_STYLE = (-16)Private Const WS_HSCROLL = &H100000Private Const WS_VSCROLL = &H200000'Used by VisibleScrollBars functionEnum Enum_VisibleScrollBars vs_none = 0 vs_vertical = 1 vs_horizontal = 2 vs_both = 4End EnumPublic Function VisibleScrollBars(ControlName As Control) _ As Enum_VisibleScrollBars ' ' Returns an enumerated type constant depicting the ' type(s) of scrollbars visible on the passed control. ' Dim MyStyle As Long MyStyle = GetWindowLong(ControlName.hWnd, GWL_STYLE) 'Use a bitwise comparison If (MyStyle And (WS_VSCROLL Or WS_HSCROLL)) = _ (WS_VSCROLL Or WS_HSCROLL) Then 'Both are visible Let VisibleScrollBars = vs_both ElseIf (MyStyle And WS_VSCROLL) = WS_VSCROLL Then 'Only Vertical is visible Let VisibleScrollBars = vs_vertical ElseIf (MyStyle And WS_HSCROLL) = WS_HSCROLL Then 'Only Horizontal is visible Let VisibleScrollBars = vs_horizontal Else 'No scrollbars are visible Let VisibleScrollBars = vs_none End IfEnd Function
Hard-coding a scrollbar with a predetermined width or height is not a good idea because these might vary depending on the user’s display settings (accessibility options, desktop themes, and so on). Call GetSystemMetrics to always ensure the proper value for this metric.