From Xojo Documentation


Canvas.Scroll(DeltaX as Integer, DeltaY as Integer, [Left as Integer], [Top as Integer], [Width as Integer], [Height as Integer], [ScrollControls as Boolean])

Supported for all project types and targets.

Scrolls the Canvas contents according to the passed parameters.


DeltaX and DeltaY are the number of points to scroll horizontally and vertically relative to the current position of the picture in the Canvas control. Positive values scroll right and down while negative values scroll left and up. The Left, Top, Width, and Height parameters specify the portion of the picture to be scrolled. If these are not passed, the entire picture will be scrolled. ScrollControls indicates whether controls positioned on top of the Canvas control should be scrolled as well. ScrollControls is True by default.

Sample Code

To use the Scroll method to scroll the contents of a Canvas control, you need to store the last scroll value for the axis you are scrolling so you can use this to calculate the amount to scroll. This can be done by adding properties to the window that contains the Canvas control or by creating a new class based on the Canvas control that contains properties to hold the last X scroll amount and last Y scroll amount.

If the ScrollControls parameter is True, any controls on top of the Canvas control will also be scrolled. This allows the implementation of a scrolling pane of controls.

The following example scrolls a picture that was added to the project. The properties XScroll and YScroll have been added to the window to hold the amounts the picture has been scrolled. The picture is scrolled 8 points at a time. In the Keydown event of the window, the following code calls the Scroll method whenever the Up, Down, Left, or Right arrow keys are pressed.

Const LeftArrow = 28
Const RightArrow = 29
Const UpArrow = 30
Const DownArrow = 31
Const ScrollUnit = 8 // points

Select Case Asc(Key)
Case LeftArrow
XScroll = XScroll + ScrollUnit
Canvas1.Scroll(ScrollUnit, 0)

Case RightArrow
XScroll = XScroll - ScrollUnit
Canvas1.Scroll(-ScrollUnit, 0)

Case UpArrow
YScroll = YScroll + ScrollUnit
Canvas1.Scroll(0, ScrollUnit)

Case DownArrow
YScroll = YScroll - ScrollUnit
Canvas1.Scroll(0, -ScrollUnit)

End Select

The Scroll method calls the Paint event of the canvas that redraws the picture with the new values of XScroll and YScroll. The Paint event has the following line of code:

g.DrawPicture(myPicture, XScroll, YScroll)