Canvas + Scrollbar: New Old Question ;)

Hello,

can’t get a working example of using Scrollbars with Canvas.
I have Picture, which will be draw into the Canvas with width e.g. 2000 and height 1500.
Have to Scroll that?

Very basic question :wink:

Add a horizontal and vertical scrollbar to beside/beneath your canvas.

In the canvas MouseWheel event add:

[code]HScrollbar.Value = HScrollbar.Value + deltaX
VScrollBar.Value = VScrollBar.Value + deltaY

me.invalidate[/code]

and in the canvas paint event (assuming you draw a picture called pic):

If pic <> Nil Then g.DrawPicture(pic, HScrollBar.Value * -1, VScrollbar.Value * -1) End If

Now you also have to specify the max values for the Scrollbars. Best to do this when you load the picture.

if pic <> Nil then HScrollbar.Maximum = pic.Width - canvas1.Width VScrollBar.Maximum = pic.Height - canvas1.Height end if

Martin, if you e-mail me at

harriew at frontiernet dot net

I can send you a little project I wrote some time ago when I was first figuring out how to scroll a canvas. In my case I am writing reports on the canvas and actually have two canvases. One holds the column headings so that they do not scroll out of site. Both canvas scroll horizontally while the canvas with the body of the report also scrolls vertically. But, once the report, or in your case, the picture is on the canvas scrolling is the same. This also allows you to vary the size of the window.