Putting a trackbar in a statusbar
Posted: Thu Oct 29, 2020 4:50 pm
Recently I needed to put a trackbar gadget in a status bar. The purpose of the trackbar gadget was to zoom in on text in an editor gadget. This was not straightforward as I found that when the main window was resized, the trackbar gadget disappeared. I could not find how to do this on the PB forums so I toyed with PB to find a solution.
I am posting my solution here in case anybody else needs to do this.
This solution is not perfect; I discovered that the status bar needs to be created after the TrackBar gadget has been created to make the trackbar gadget stay on top of the status bar. This led me to overcome the problem of the trackbar disappearing when the main window is resized by first resizing the Trackbar gadget then freeing the status bar and then recreating the status bar. This is a clumsy solution. I would be interested to know if anybody can improve on it. I think it is possible to avoid this in VB by making the trackbar control a child of the status window but I could not achieve this in PB.
I am posting my solution here in case anybody else needs to do this.
Code: Select all
EnableExplicit
Enumeration
#MainWindow
#StatusBar
#EditorGadget
#TrackBarGadget
EndEnumeration
Global StatusRect.RECT, hEdit, hStatus
Global Maxrange, MaxD, sliderValue
Global EventID
Procedure MakeStatusBar()
Protected hStatus
hStatus = CreateStatusBar(#StatusBar, WindowID(#MainWindow))
AddStatusBarField(90)
AddStatusBarField(100)
AddStatusBarField(200)
AddStatusBarField(#PB_Ignore) ; automatically resize this field
StatusBarText(#StatusBar, 0, "First bar field")
StatusBarText(#StatusBar, 1, "Second bar field")
ProcedureReturn hStatus
EndProcedure
Procedure SizeWindowHandler()
Protected statusHeight
Shared hStatus
statusHeight= StatusBarHeight(#StatusBar)
SendMessage_(hStatus, #SB_GETRECT, 2, StatusRect) ;2 refs the third panel in the statusbar
ResizeGadget(#TrackBarGadget, #PB_Ignore, WindowHeight(#MainWindow)-StatusBarHeight(#StatusBar)+5, #PB_Ignore, #PB_Ignore)
ResizeGadget(#EditorGadget, #PB_Ignore, #PB_Ignore, WindowWidth(#MainWindow), WindowHeight(#MainWindow)-StatusBarHeight(#StatusBar))
FreeStatusBar(#StatusBar)
hStatus = MakeStatusBar()
EndProcedure
OpenWindow(#MainWindow, 100, 200, 600, 400, "Gadget on status bar", #PB_Window_MinimizeGadget| #PB_Window_SizeGadget |#PB_Window_SizeGadget)
Maxrange=400
MaxD=Maxrange/64 +1
TrackBarGadget(#TrackBarGadget, 197, WindowHeight(#MainWindow)-20, 195, 20, 1, Maxrange )
hStatus = MakeStatusBar()
hEdit=EditorGadget(#EditorGadget,1,0,WindowWidth(#MainWindow), WindowHeight(#MainWindow)-StatusBarHeight(#StatusBar))
SetGadgetText(#EditorGadget, "Use the slider to resize the text!")
BindEvent(#PB_Event_SizeWindow, @SizeWindowHandler())
Repeat
EventID = WaitWindowEvent()
If EventID = #PB_Event_Gadget
Select EventGadget()
Case #TrackBarGadget
sliderValue=GetGadgetState(#TrackBarGadget)
If SendMessage_(hEdit,#EM_SETZOOM,sliderValue,MaxD) = #False
Debug "Error, Cannot set new zooming Range"
EndIf
EndSelect
EndIf
Until EventID = #PB_Event_CloseWindow