This content is provided AS IS for archive purposes only. Content may be out of date with no guarantee of correctness.

Scrollbars - Elements - Hag GUI for Cobra - Static

Main Article

Scrollbars - Elements - Hag GUI for Cobra

Scrollbars using the XP theme.

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.


Program (icon:"hag\icons\window.ico")

   IMG_HEIGHT = 1024

   frmMain: ^forms
       btnClose: ^buttons
       sbrMain: ^scrollBars
       cvsMain: ^canvases
   largeImage: Element
   background: Element
   x, y: Integer = 1


   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
   HagInit(320, 240, 1) // Start at sprite index because of the background sprite    
   // 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)

   // 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")
   While ExitRequested = FALSE
       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))+"%")
ButtonClicked(btnClose) then RequestExit

       If KeyHits(VK_ESCAPE) > 0 then RequestExit


Stay Subscribed

@golightlyb, subscribe to the RSS feed or get updates by e-mail.

You can also contact me directly - I make an effort to reply to every e-mail.