October 25, 2016
Hot Topics:

Using Scrollbars with Picture Boxes

  • November 18, 2002
  • By Developer.com Staff
  • Send Email »
  • More Articles »

Many programmers have problems with pictureboxes and scrollbars. Here's a solution for that problem:

1. Add to Form1: two pictureboxes (Picture1, Picture2) and one vertical scrollbar (VScroll1). The positions doesn't matter, the objects are positioned automatically by the program. There's only one property that must be set at designtime: AutoRedraw = True of Picture2 (that's very important)!

2. Add following code to Form1

Private Sub Form_Activate()

'Clip image of picture2 to picture1
ScrollPic Picture2, Picture1, VScroll1

End Sub

Private Sub Form_Load()

'Set the properties and positions 
'of the objects
With Me
.Height = 5000
.Width = 6500
End With

With VScroll1
.Left = 3200
.Top = 200
.Height = 1000
End With

With Picture1
.Left = 200
.Top = 200
.Width = 3000
.Height = 1000
End With

'AutoRedraw must be set to True!!!
With Picture2
.Left = 200
.Top = 1500
.Width = 3000
.Height = 2500
.Visible = False
End With

'Variables needed
TwipsX = Screen.TwipsPerPixelX
TwipsY = Screen.TwipsPerPixelY
DestPicx = Picture2.Width / TwipsX
DestPicY = Picture2.Height / TwipsY
VScroll1.Max = Picture2.Height / TwipsY _
- Picture1.Height / TwipsY

'Print A to L on Picture2
Dim i As Integer

For i = 0 To 11
Picture2.Print Chr$(65 + i)
Next i

End Sub
Private Sub VScroll1_Change()

ScrollPic Picture2, Picture1, VScroll1

End Sub
Private Sub VScroll1_Scroll()

ScrollPic Picture2, Picture1, VScroll1

End Sub
3. Add a module with following code:

Declare Function BitBlt Lib "gdi32" _
(ByVal hDestDC As Integer, ByVal x As Integer, _
ByVal y As Integer, ByVal nWidth As Integer, _
ByVal nHeight As Integer, ByVal _
hSrcDC As Integer, ByVal xSrc As Integer, _
ByVal ySrc As Integer, ByVal dwRop As _
Long) As Integer
Public Const SRCCOPY = &HCC0020
Public Const SRCAND = &H8800C6
Public Const SRCINVERT = &H660046

Public TwipsX As Long
Public TwipsY As Long
Public DestPicx As Long
Public DestPicY As Long

Public Sub ScrollPic(SourcePic As Object, _
DestPic As Object, Scroll As Object)

BitBlt DestPic.hDC, 0&, 0&, DestPicx, DestPicY, _
SourcePic.hDC, 0&, Scroll.Value, SRCCOPY

End Sub

Hope that it'll help many programmers!

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel