From Xojo Documentation

Revision as of 00:48, 13 February 2022 by Gperlman (talk | contribs) (Sample Code)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

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

New in 2021r3

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 DesktopCanvas 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 DesktopCanvas control should be scrolled as well. ScrollControls is True by default.

Sample Code

To use the Scroll method to scroll the contents of a DesktopCanvas 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 DesktopCanvas 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 DesktopCanvas 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 Key.Asc
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)