精确控制滚动条的显示
Author: Steffen Ploetz
The thumb width is a function of ScrollBar.
PageStep and ScrollBar.
MinValue and ScrollBar.
Value and it is hard to control. The following code snippet translate relative values (range 0.. ... 1.0) to the ScrollBar properties...
'' Sets the thumb width and scroll value as relative value in the range between 0.0 (lowest possible value) and 1.0 (highest possible value).
''
'' The **hScrollBar** argument defines the scroll bar to set the thumb width and scroll value for.
'' The **fThumb** argument defines the thumb width to set as relative value in the range between 0.0 and 1.0.
'' The **fValue** argument defines the scroll value to set as relative value in the range between 0.0 and 1.0.
'' Remarks: Good results can be achieved for scroll bars with these initial property values: MaxValue=100000, MinValue=500, PageStep=500, Step=500
Private Sub SetThumbAndScrollValue(hScrollBar As ScrollBar, fThumb As Float, fValue As Float)
Dim fThumbMin As Float = 0.005 ' The thumb should not be smaller than 5% of the extent of the scroll bar.
Dim iMax As Integer = hScrollBar.MaxValue
If fThumb < fThumbMin Then fThumb = fThumbMin
If fThumb > 1.0 Then fThumb = 1.0
If fValue < 0.0 Then fValue = 0.0
If fValue > 1.0 Then fValue = 1.0
hScrollBar.PageStep = fThumb * iMax
hScrollBar.MinValue = fThumb * iMax
hScrollBar.Value = fThumb * iMax + (iMax - hScrollBar.MinValue) * fValue
End
The following code snippet translates the ScrollBar properties into a relative value (range 0.0 ... 1.0)...
Note that the static variable
fOldVal
only exists once and the function can therefore only ever be applied to one and the same ScrollBar.
'' Determines the relative scroll value in the range between 0.0 (lowest possible value) and 1.0 (highest possible value).
''
'' The **hScrollBar** argument defines the scroll bar to get the scroll value for.
'' Returns: The relative value in the range between 0.0 and 1.0.
'' Remarks: Good results can be achieved for scroll bars with with these initial property values: MaxValue=100000, MinValue=500, PageStep=500, Step=500
Private Function GetScrollValue(hScrollBar As ScrollBar) As Float
Dim iMaxValue As Integer = hScrollBar.MaxValue
Dim iMinValue As Integer = hScrollBar.MinValue
Dim fThumbVal As Float = hScrollBar.PageStep / iMaxValue
Dim fValue As Float = 0
Static fOldVal As Float = 0 ' Avoid unpredictability.
If iMaxValue - iMinValue > 0.0 Then
fValue = Max(0, (hScrollBar.Value - fThumbVal * iMaxValue) / (iMaxValue - iMinValue))
fOldVal = fValue
Else
fValue = fOldVal
Endif
Return fValue
End
Page revisions
Precise control of the display of a scroll bar: Steffen Ploetz - April 25th, 2024