|
-
Mar 21st, 2003, 03:51 PM
#1
Thread Starter
Frenzied Member
VB - Scrollable picture box
Here's how to make a scrollable picture box.- Drop a picture box on the form
- Size it to your desired size
- Drop another picture box INSIDE the first one
- Drop a VScrollBar INSIDE the first PictureBox
- Drop an HScrollBar INSIDE the FIRST PictureBox
- Drop a TextBox INSIDE the FIRST PictureBox
- Paste the code below into your form window
- Either change the path for loading the PictureBox Picture in code, or remove it and set the Picture property manually
- Run the program and you will have a scrolling picture box
- The easiest way to put the controls inside the first picture box (IMHO) is to drop them on the form, then select them all, cut them, select the first picture box, and then paste the controls inside.
You don't have to worry about the location of any controls except the first (container) PictureBox. The code does the rest:
VB Code:
Option Explicit
Private Sub Form_Load()
' Setup picture box
With Picture2
.AutoSize = True
.ZOrder 1
.Picture = LoadPicture("C:\MyPicture.jpe")
.Move 0, 0
End With
' Setup textbox
With Text1
.BorderStyle = 0
.BackColor = Me.BackColor
.Enabled = False
.Height = HScroll1.Height
.Text = ""
.Width = VScroll1.Width
End With
' Setup scroll bars
ArrangeScrollBars
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.MousePointer = vbDefault
End Sub
Private Sub Form_Resize()
' Adjust scroll bars to new form size
ArrangeScrollBars
End Sub
Private Sub ArrangeScrollBars()
' Resize scroll bars to fit form, and set their max value (in relation to form size)
With HScroll1
.Move 0, Picture1.ScaleHeight - .Height, Picture1.ScaleWidth - VScroll1.Width
.Max = IIf((Picture2.ScaleWidth - Picture1.Width + VScroll1.Width) > 0, _
Picture2.Width - Picture1.ScaleWidth + VScroll1.Width, 0)
.SmallChange = IIf(Int(.Max / 10) > 0, Int(.Max / 10), 1)
.LargeChange = IIf(Int(.Max / 5) > 0, Int(.Max / 5), 1)
End With
With VScroll1
.Move Picture1.ScaleWidth - .Width, 0, .Width, Picture1.ScaleHeight - HScroll1.Height
.Max = IIf((Picture2.Height - Picture1.ScaleHeight + HScroll1.Height) > 0, _
Picture2.Height - Picture1.ScaleHeight + HScroll1.Height, 0)
.SmallChange = IIf(Int(.Max / 10) > 0, Int(.Max / 10), 1)
.LargeChange = IIf(Int(.Max / 5) > 0, Int(.Max / 5), 1)
End With
With Text1
.Left = HScroll1.Width
.Top = VScroll1.Height
.ZOrder 0
End With
End Sub
Private Sub HScroll1_Change()
Picture2.Move 0 - HScroll1.Value
End Sub
Private Sub HScroll1_Scroll()
Picture2.Move 0 - HScroll1.Value
End Sub
Private Sub VScroll1_Change()
Picture2.Move Picture2.Left, 0 - VScroll1.Value
End Sub
Private Sub VScroll1_Scroll()
Picture2.Move Picture2.Left, 0 - VScroll1.Value
End Sub
Last edited by seaweed; Mar 21st, 2003 at 03:57 PM.
~seaweed
-
Mar 22nd, 2003, 01:21 AM
#2
Frenzied Member
might i suggest
VB Code:
Private Sub ArrangeScrollBars()
' Resize scroll bars to fit form, and set their max value (in relation to form size)
With HScroll1
.Move 0, Picture1.ScaleHeight - .Height, Picture1.ScaleWidth - VScroll1.Width
.Max = IIf((Picture2.ScaleWidth - Picture1.Width + VScroll1.Width) > 0, _
Picture2.Width - Picture1.ScaleWidth + VScroll1.Width, 0)
.SmallChange = IIf(Int(.Max / 10) > 0, Int(.Max / 10), 1)
.LargeChange = IIf(Int(.Max / 5) > 0, Int(.Max / 5), 1)
End With
With VScroll1
.Move Picture1.ScaleWidth - .Width, 0, .Width, Picture1.ScaleHeight - HScroll1.Height
.Max = IIf((Picture2.Height - Picture1.ScaleHeight + HScroll1.Height) > 0, _
Picture2.Height - Picture1.ScaleHeight + HScroll1.Height, 0)
.SmallChange = IIf(Int(.Max / 10) > 0, Int(.Max / 10), 1)
.LargeChange = IIf(Int(.Max / 5) > 0, Int(.Max / 5), 1)
End With
With Text1
.Left = HScroll1.Width
.Top = VScroll1.Height
.ZOrder 0
End With
'-- remove scroll bars if theyre not needed--
If HScroll1.Max = 0 Then
HScroll1.Visible = False
Else
HScroll1.Visible = True
End If
If VScroll1.Max = 0 Then
VScroll1.Visible = False
Else
VScroll1.Visible = True
End If
End Sub
-
Apr 14th, 2003, 06:06 PM
#3
Thread Starter
Frenzied Member
Also, adding:
VB Code:
Picture1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
to the form's Load and Resize event complete the project to give you a sizeable, scrollable picture.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|