Scrolling in a picturebox

boroangel
01-03-2005, 10:03 AM
Ok I met another problem and I need some help...I am trying to input a picture into a picturbox but the pic is too large for teh boundary of the picturebox...therefore I need scrollbars(horizontal and vertical) to that the user can scroll and see the whole picture. I search teh msdn library and used this example...

I drew 1 picturebox and drew another picturebox inside it...
then i doubleclick on teh form and put in this code....

Private Sub Form_Load()

' Set ScaleMode to pixels.
Form1.ScaleMode = vbPixels
Picture1.ScaleMode = vbPixels

' Autosize is set to True so that the boundaries of
' Picture2 are expanded to the size of the actual
' bitmap.
Picture2.AutoSize = True

' Set the BorderStyle of each picture box to None.
Picture1.BorderStyle = 0
Picture2.BorderStyle = 0

' Load the bitmap.
Picture2.Picture = _
LoadPicture("C:\P1010172.JPG")

' Initialize location of both pictures.
Picture1.Move 0, 0, ScaleWidth - VScroll1.Width, _
ScaleHeight - HScroll1.Height
Picture2.Move 0, 0

' Position the horizontal scroll bar.
HScroll1.Top = Picture1.Height
HScroll1.Left = 0
HScroll1.Width = Picture1.Width

' Position the vertical scroll bar.
VScroll1.Top = 0
VScroll1.Left = Picture1.Width
VScroll1.Height = Picture1.Height

' Set the Max property for the scroll bars.
HScroll1.Max = Picture2.Width - Picture1.Width
VScroll1.Max = Picture2.Height - Picture1.Height

' Determine if the child picture will fill up the
' screen.
' If so, there is no need to use scroll bars.
VScroll1.Visible = (Picture1.Height < _
Picture2.Height)
HScroll1.Visible = (Picture1.Width < _
Picture2.Width)

End Sub

But it did not work....can anyone try running it?

noi_max
01-03-2005, 11:08 AM
Hi there!

It seems what's missing is the Scrollbars change event.

I didn't search for the example, but it wasn't to difficult to figure out the change events.

I put everything into pixel scale, including the max value of the scrollbar as you'll see in the code.

I commented out the LoadPicture function because I was setting the Picture property manually for quick testing.

This seems to work...

Option Explicit

Private Sub Form_Load()

' Set ScaleMode to pixels.
Form1.ScaleMode = vbPixels
Picture1.ScaleMode = vbPixels
Picture2.ScaleMode = vbPixels

' Autosize is set to True so that the boundaries of
' Picture2 are expanded to the size of the actual
' bitmap.
Picture2.AutoSize = True

' Set the BorderStyle of each picture box to None.
Picture1.BorderStyle = 0
Picture2.BorderStyle = 0

' Load the bitmap. (I have this commented out as I loaded my own pic in the
' picture property manually for testing)

'Picture2.Picture = LoadPicture("C:\P1010172.JPG")

' Initialize location of both pictures.
Picture1.Move 0, 0, ScaleWidth - VScroll1.Width, _
ScaleHeight - HScroll1.Height
Picture2.Move 0, 0

' Position the horizontal scroll bar.
HScroll1.Top = Picture1.Height
HScroll1.Left = 0
HScroll1.Width = Picture1.Width

' Position the vertical scroll bar.
VScroll1.Top = 0
VScroll1.Left = Picture1.Width
VScroll1.Height = Picture1.Height

' Set the Max property for the scroll bars (in pixels)
HScroll1.Max = Picture2.ScaleWidth - Picture1.ScaleWidth
VScroll1.Max = Picture2.ScaleHeight - Picture1.ScaleHeight

' Determine if the child picture will fill up the
' screen.
' If so, there is no need to use scroll bars.
VScroll1.Visible = (Picture1.Height < _
Picture2.Height)
HScroll1.Visible = (Picture1.Width < _
Picture2.Width)

End Sub

Private Sub HScroll1_Change()

'To move left, use the negative of the scroll value
Picture2.Left = -(HScroll1.Value)

End Sub

Private Sub VScroll1_Change()

'To move up, use the negative of the scroll value
Picture2.Top = -(VScroll1.Value)

End Sub


Hope that helps!

zelg37
01-03-2005, 09:58 PM
Instead of raw Msdn code you might have just tried searched for a working picturebox-inside-picturebox scrolling examples on the forum. This one has code to automatically decide if scrollbars are needed depending on the resizing of the form:
http://www.xtremevbtalk.com/showpost.php?p=533456

boroangel
01-09-2005, 04:29 AM
I have got it working...thanks guys

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum