Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Feb 24, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Determine Which Scrollbars are Visible

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 Constants
Private Const GWL_STYLE = (-16)
Private Const WS_HSCROLL = &H100000
Private Const WS_VSCROLL = &H200000

'Used by VisibleScrollBars function
Enum Enum_VisibleScrollBars
	vs_none = 0
	vs_vertical = 1
	vs_horizontal = 2
	vs_both = 4
End Enum

Public 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)) = _
		'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
		'No scrollbars are visible
		Let VisibleScrollBars = vs_none
	End If
End 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.
Michael B.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date