Scrollbars - Elements - Hag GUI for Cobra
Scrollbars allow the user to "scroll" the display through items that do not fit into a viewport or window.
Scrollbars expose the following read-only variables:
scrollbar.x: Integer - Scrollbar x position
scrollbar.y: Integer - Scrollbar y position
scrollbar.w: Integer - Scrollbar width
scrollbar.h: Integer - Scrollbar height
scrollbar.enabled: Boolean - If scrollbar is enabled
scrollbar.visible: Boolean - If scrollbar is visible
scrollbar.gotUpdated: Boolean - If scrollbar got updated
scrollbar.sbrOffset: Integer - The current offset scrolled by
scrollbar.sbrLength: Integer - The length of the scrollable element
For a ready-to-run example, see Example_Scrollbar in the Examples download.
Example:
Uses
hagC2D,
cobra2D,
keyset
Const
IMG_HEIGHT = 1024
Var
frmMain: ^forms
btnClose: ^buttons
sbrMain: ^scrollBars
cvsMain: ^canvases
largeImage: Element
background: Element
x, y: Integer = 1
Begin
SetAppName("Scrollbar example")
OpenScreen(320, 240, 32, FALSE, COB_SHOWBORDER+COB_SHOWCLOSE)
background = CreateSprite(320,240)
Cls(ToRGBA(235,233,237),background) ; Flip
// Init GUI
HagBaseDir("hag\")
HagInit(320, 240, 1) // Start at sprite index because of the background sprite
HagLoadGuiTheme("xp")
// Create a really large image to draw to the canvas sprite
largeImage = CreateImage(270, IMG_HEIGHT)
CLS(ToRGBA(255,255,255), largeImage)
For x = -20 to 270 Step 20
For y = -20 to IMG_HEIGHT Step 20
Rect(x, y, Rand(1, 40), Rand(1, 40), ToRGBA(Rand(0,255),Rand(0,255),Rand(0,255),Rand(25,255)), TRUE, largeImage)
Next
Next
// Set up GUI Elements
frmMain = CreateForm()
cvsMain = CreateCanvas(frmMain,20,20,270,180)
sbrMain = CreateScrollBar(frmMain, 289, 21, 178)
ScrollBarSetLength(sbrMain, IMG_HEIGHT)
ScrollBarSetSpeed(sbrMain, 5)
SetScrollBarMouseWheelTargetElement(sbrMain, cvsMain.e)
btnClose = CreateButton(frmMain, 250, 207, 50, "Close")
FocusButton(btnClose)
HagUpdateOnce()
While ExitRequested = FALSE
HagUpdateAll_AutoKeys()
If ScrollBarUpdated(sbrMain) then
Rect(0, 0, 270, 180, ToRGBA(255,255,255), TRUE, cvsMain.e)
DrawImage(largeImage, 0, 0 - scrollBarOffset(sbrMain), cvsMain.e)
Rect(0, 0, 270, 180, ToRGBA(0,0,0), FALSE, cvsMain.e)
SetAppName("ScrollBar Percent: "+ToInt(ScrollBarPercent(sbrMain))+"%")
Endif
If ButtonClicked(btnClose) then RequestExit
If KeyHits(VK_ESCAPE) > 0 then RequestExit
Flip
Pause(1)
Wend
HagFreeAll()
CloseScreen()
End
Stay Subscribed
RSS feed or get updates by e-mail.
, subscribe to theYou can also contact me directly - I make an effort to reply to every e-mail.