You don't need to use a thread to get the live position of the ScrollBar. In WindowsPolo wrote:At the moment, the only way to get the live position of the Scrollbar is to use a thread (see link below), although this is not very convenient.
viewtopic.php?f=13&t=46396
you can utilize a window callback or subclass the ScrollBarGadget. And in Linux
and MacOS X it isn't too difficult neither. Take a look into this cross-platform
example of using a ScrollBar with live scrolling to scroll an image. I have tested it
with PB 4.51 in Windows 98 SE, Windows XP SP2 + SP3, Windows 7 x86 and x64,
andLinux/Kubuntu 9.04 and MacOS X 10.6.7 (Snow Leopard):
Code: Select all
EnableExplicit
Define ScrollStep.F
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
ProcedureC ScrollBarCallback(*Range.GtkRange, ScrollType.L, NewValue.D, UserData.L)
Shared ScrollStep.F
GrabImage(0, 1, Int(GetGadgetState(1) * ScrollStep), 0, GadgetWidth(0) - 4, ImageHeight(0))
SetGadgetState(0, ImageID(1))
EndProcedure
CompilerCase #PB_OS_MacOS
ImportC ""
CreateScrollBarControl(WindowRef.L, *BoundsRect, CurrentValue.L, MinimumValue.L, MaximumValue.L, ViewSize.L, LiveTracking.L, LiveTrackingProc.L, *ControlRef)
GetControl32BitMaximum(ControlRef.L)
GetControl32BitMinimum(ControlRef.L)
GetControl32BitValue(ControlRef.L)
SetControl32BitValue(ControlRef.L, NewValue.L)
ShowControl(ControlRef.L)
EndImport
#kControlDownButtonPart = 21
#kControlUpButtonPart = 20
Structure Rect
Top.W
Left.W
Bottom.W
Right.W
EndStructure
ProcedureC ScrollBarCallback(ControlRef.L, PartCode.L)
Shared ScrollStep.F
Protected CurrentPosition.L
Select PartCode
Case #kControlUpButtonPart
CurrentPosition = GetControl32BitValue(ControlRef)
If CurrentPosition > GetControl32BitMinimum(ControlRef)
SetControl32BitValue(ControlRef.L, CurrentPosition - Int(ScrollStep))
EndIf
Case #kControlDownButtonPart
CurrentPosition = GetControl32BitValue(ControlRef)
If CurrentPosition < GetControl32BitMaximum(ControlRef)
SetControl32BitValue(ControlRef.L, CurrentPosition + Int(ScrollStep))
EndIf
EndSelect
GrabImage(0, 1, Int(GetControl32BitValue(ControlRef) * ScrollStep), 0, GadgetWidth(0) - 4, ImageHeight(0))
SetGadgetState(0, ImageID(1))
EndProcedure
CompilerCase #PB_OS_Windows
Procedure WindowCallback(WindowHandle.I, Msg.I, WParam.I, LParam.I)
Shared ScrollStep.F
If LParam = GadgetID(1)
If Msg = #WM_HSCROLL
GrabImage(0, 1, Int(GetGadgetState(1) * ScrollStep), 0, GadgetWidth(0) - 4, ImageHeight(0))
SetGadgetState(0, ImageID(1))
EndIf
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
CompilerEndSelect
If LoadImage(0, #PB_Compiler_Home + "/examples/sources/Data/PureBasicLogo.bmp")
OpenWindow(0, 200, 100, 172, 100, "Live tracking", #PB_Window_SystemMenu)
ImageGadget(0, 5, 5, WindowWidth(0) - 9, ImageHeight(0), 0, #PB_Image_Border)
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
Define Bounds.Rect
Define ScrollBarRef.L
Bounds\Left = 5
Bounds\Top = WindowHeight(0) - 20
Bounds\Right = WindowWidth(0) - 5
Bounds\Bottom = WindowHeight(0) - 5
If CreateScrollBarControl(WindowID(0), @Bounds, 0, 0, GadgetWidth(0), GadgetWidth(0), #True, @ScrollBarCallback(), @ScrollBarRef) = 0
ShowControl(ScrollBarRef)
GrabImage(0, 1, GetControl32BitValue(ScrollBarRef), 0, GadgetWidth(0) - 4, ImageHeight(0))
ScrollStep = (ImageWidth(0) - GadgetWidth(0)) / GadgetWidth(0)
EndIf
CompilerElse
Define ViewSize.I
ScrollStep = ImageWidth(0) / GadgetWidth(0)
ViewSize = Int(0.0 + GadgetWidth(0) / ImageWidth(0) * GadgetWidth(0))
ScrollBarGadget(1, 5, WindowHeight(0) - 20, WindowWidth(0) - 10, 15, 0, GadgetWidth(0), ViewSize)
GrabImage(0, 1, GetGadgetState(1), 0, GadgetWidth(0) - 4, ImageHeight(0))
CompilerEndIf
SetGadgetState(0, ImageID(1))
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
g_signal_connect_data_(GadgetID(1), "change-value", @ScrollBarCallback(), 0, 0, 0)
CompilerCase #PB_OS_Windows
SetWindowCallback(@WindowCallback(), 0)
CompilerEndSelect
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf