Making a MaxGUI Application: StripAnimMakerLite Part 3 - Scrolling Canvas Image
(c) Assari Jan 16 2005

Now that we have the capability to load and display the loaded image on our canvas, let us add in the ability to scroll to our loaded image if the image is bigger than the 480x400 canvas.

In order to do this, we need to do the following:-

New Additions to Part 1 - Variable Definitions

We need 4 new variables, Hscroll and VScroll for our scroller gadgets and 2 integer variables (sh, sv) to hold the slider values

    Global HScroll:TGadget     'Horizontal Scroll Gadget
Global sh:Int, sv:Int      'Scroller Horizontal and Vertical Values
Global VScroll:TGadget     
'Vertical Scroll Gadget

New Additions to Part 2 - Gadget Creation Block

The we need to create the scroller gadgets

    '--------------------------------------------------------
'Create Vertical and Horizontal Scrollers for Main Canvas
'--------------------------------------------------------
CreateScrollers()


New Additions to Part 3 - MAIN LOOP

In the main loop we need to add in the capability to process the Gadget events returned by our scroll bars
        Case EVENT_GADGETACTION
        Process_GadgetActionEvent


New Additions to Part 4 - Function Definitions

In this block we need to do the following
CreateScrollers()

The first function required is to create the required Scrollers, one for the horizontal one for the vertical. Nothing fancy here. We just need to be careful how we position the scrollers.
    '---------------------------------------------
'Create Vertical and Horizontal Scrollers for Main Canvas
'---------------------------------------------
Function CreateScrollers()

  Local CW:int = 
GadgetWidth(Canvas1)
  Local CH:int =
GadgetHeight(Canvas1)

  
VScroll:TGadget=CreateSlider(CW,0,20,CH,MyWindow,SLIDER_VERTICAL)
  HScroll:TGadget=CreateSlider(0,CH,CW,20,MyWindow,SLIDER_HORIZONTAL)

End Function

The Process EVENT_GADGETACTION function

This function checks the gadget source, if it was the scroll bars, we refill our temporary Pixmap Pix using the  most current vertical and horizontal slider values and then redraw our canvas with the new image.
    '---------------------------------------------
'Function to Process EVENT_GADGETACTION
'---------------------------------------------
Function Process_GadgetActionEvent:Int()

 Select EventSource()
     Case HScroll
        sh=EventData()
        pix:TPixmap=PixmapWin(PixMap1,sh,sv)
        RedrawGadget(Canvas1)
     Case VScroll
        sv=EventData()
        pix:TPixMap=PixmapWin(PixMap1,sh,sv)
        RedrawGadget(Canvas1)
 End Select  

End Function



And then lastly we need to modify the LoadCanvasImage function to set the slider range.
    '---------------------------------------------
'Load the image - later on we will put in a File Requester
'Make a copy of the Pixmap for the canvas
'---------------------------------------------
Function LoadCanvasImage(file:String="")

  If file="" file=RequestFile("Load Image File")
  PixMap1:TPixmap=LoadPixmap(file)
  If PixMap1=Null Then
    PixMap1=LoadPixmap("1945.bmp") 'loads default if fails
  EndIf
  Pix:TPixmap=PixmapWin(PixMap1,0,0)

'--------------------------------------------------------
'Since we know the size of the image we can set the slider range
'--------------------------------------------------------
  SetSliderRange VScroll,GadgetHeight(Canvas1),PixmapHeight(PixMap1)
  SetSliderRange HScroll,GadgetWidth(Canvas1),PixmapWidth(PixMap1)
 
End Function


That was short and I hope painless. The full source code can be downloaded here. Once we have made the above modifications and then compile and run the program we should see the following (note the scrollbars):-

   




That's all for now. Return to Main Index.