Results 1 to 17 of 17

Thread: Scroll bar in picturebox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2010
    Posts
    18

    Scroll bar in picturebox

    I have a scanned image that is bigger than the screen when it gets displayed on the screen. I don't really want to size down the image so I would like to be able to add scroll bars to the image. How do I go about adding a scroll bar to a picturebox so it will scroll the image when I move the scroll bar?

  2. #2

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2010
    Posts
    18

    Re: Scroll bar in picturebox

    It seems like I have did this before, several years back but I don't remember how I did it. How are you 'attaching' picPicture to picWindow?

    I know if I use what you wrote and I scan in an 8.5x11 page I can scroll as long as I'm scanning in color. I want to scan in black and white. When I do that I end up getting and Overflow error highlighted on VScroll1.Max =. I'm not using two pictureboxes rather only one with the scroll bars setting at the edge of the screen. I'm not sure if having the extra picturebox would take care of the problem or not but to try I would have to figure out how to 'attach' one to the other first.

    Thanks,

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Scroll bar in picturebox

    You have to use two pictureboxes in order to make the scrolling work. The idea is that the scrollbars move the "inner" picturebox -- the one that holds the image -- around inside the "outer" picturebox. If you only use one picturebox, when you scroll it it'll look cheesy and amateurish as it clearly slides under the scrollbars. (Or worse, slides over the scrollbars.)

    The scrollbars stay outside both pictureboxes for that exact reason.

  5. #5
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Scroll bar in picturebox

    Here's generic code to handle pretty much everything you need. The control names:

    picScroll: Outer picturebox that handles the scrolling.
    picClient: Inner picturebox with the actual image. Make its container picScroll by copy & pasting it.
    scrHorizontal: Horizontal scroll bar.
    scrVertical: Vertical scroll bar.

    The scroll bars should be on the form, not inside any picturebox.
    vb Code:
    1. Private Sub Form_Resize()
    2.     Dim lngWidth As Long
    3.     Dim lngHeight As Long
    4.     Dim blnAdjusting As Boolean
    5.     Dim blnScrollX As Boolean
    6.     Dim blnScrollY As Boolean
    7.     Dim lngBorderWidth As Long
    8.     Dim lngBorderHeight As Long
    9.    
    10.     With Me
    11.         ' Client border dimensions
    12.         lngBorderWidth = (.picScroll.Width - .picScroll.ScaleWidth)
    13.         lngBorderHeight = (.picScroll.Height - .picScroll.ScaleHeight)
    14.         ' Initialize to maximum displayable client size
    15.         lngWidth = .ScaleWidth - lngBorderWidth
    16.         lngHeight = .ScaleHeight - lngBorderHeight
    17.         ' Identify which scrollbars are necessary
    18.         Do
    19.             blnAdjusting = False
    20.             If .picClient.Width > lngWidth And Not blnScrollX Then
    21.                 lngHeight = lngHeight - .scrHorizontal.Height
    22.                 blnScrollX = True
    23.                 blnAdjusting = True
    24.             End If
    25.             If .picClient.Height > lngHeight And Not blnScrollY Then
    26.                 lngWidth = lngWidth - .scrVertical.Width
    27.                 blnScrollY = True
    28.                 blnAdjusting = True
    29.             End If
    30.         Loop While blnAdjusting
    31.         ' Add border size back in
    32.         lngWidth = lngWidth + lngBorderWidth
    33.         lngHeight = lngHeight + lngBorderHeight
    34.         If lngWidth < 0 Then lngWidth = 0
    35.         If lngHeight < 0 Then lngHeight = 0
    36.         ' Set client area
    37.         .picScroll.Move 0, .picHeader.Height, lngWidth, lngHeight
    38.         ' Set scrollbars
    39.         .scrHorizontal.Move 0, .ScaleHeight - .scrHorizontal.Height, lngWidth, .scrHorizontal.Height
    40.         If .scrHorizontal.Value <> 0 Then .scrHorizontal.Value = 0
    41.         If blnScrollX Then
    42.             .scrHorizontal.Max = .picClient.ScaleWidth - .picScroll.ScaleWidth
    43.             .scrHorizontal.LargeChange = .picScroll.ScaleWidth
    44.         End If
    45.         If .scrHorizontal.Visible <> blnScrollX Then .scrHorizontal.Visible = blnScrollX
    46.         .scrVertical.Move .ScaleWidth - .scrVertical.Width, 0, .scrVertical.Width, lngHeight
    47.         If .scrVertical.Value <> 0 Then .scrVertical.Value = 0
    48.         If blnScrollY Then
    49.             .scrVertical.Max = .picClient.ScaleHeight - .picScroll.ScaleHeight
    50.             If .picScroll.ScaleHeight < Screen.TwipsPerPixelY Then
    51.                 .scrVertical.LargeChange = Screen.TwipsPerPixelY
    52.             Else
    53.                 .scrVertical.LargeChange = .picScroll.ScaleHeight
    54.             End If
    55.         End If
    56.         If .scrVertical.Visible <> blnScrollY Then .scrVertical.Visible = blnScrollY
    57.     End With
    58. End Sub
    59.  
    60. Private Sub scrHorizontal_Change()
    61.     ScrollX Me.scrHorizontal.Value
    62. End Sub
    63.  
    64. Private Sub scrHorizontal_GotFocus()
    65.     Me.picScroll.SetFocus
    66. End Sub
    67.  
    68. Private Sub scrHorizontal_Scroll()
    69.     ScrollX Me.scrHorizontal.Value
    70. End Sub
    71.  
    72. Private Sub scrVertical_Change()
    73.     ScrollY Me.scrVertical.Value
    74. End Sub
    75.  
    76. Private Sub scrVertical_GotFocus()
    77.     Me.picScroll.SetFocus
    78. End Sub
    79.  
    80. Private Sub scrVertical_Scroll()
    81.     ScrollY Me.scrVertical.Value
    82. End Sub
    83.  
    84. Private Sub ScrollX(plngScrollX As Long)
    85.     Me.picClient.Left = -plngScrollX
    86. End Sub
    87.  
    88. Private Sub ScrollY(plngScrollY As Long)
    89.     Me.picClient.Top = -plngScrollY
    90. End Sub
    Those focus events are to suppress the tendency of VB6 scrollbars to do this annoying flashing thing when they have the focus.

  6. #6
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    723

    Question Re: Scroll bar in picturebox

    picHeader?

    Quote Originally Posted by Ellis Dee View Post
    Here's generic code to handle pretty much everything you need. The control names:

    picScroll: Outer picturebox that handles the scrolling.
    picClient: Inner picturebox with the actual image. Make its container picScroll by copy & pasting it.
    scrHorizontal: Horizontal scroll bar.
    scrVertical: Vertical scroll bar.

    The scroll bars should be on the form, not inside any picturebox.
    vb Code:
    1. Private Sub Form_Resize()
    2.     Dim lngWidth As Long
    3.     Dim lngHeight As Long
    4.     Dim blnAdjusting As Boolean
    5.     Dim blnScrollX As Boolean
    6.     Dim blnScrollY As Boolean
    7.     Dim lngBorderWidth As Long
    8.     Dim lngBorderHeight As Long
    9.    
    10.     With Me
    11.         ' Client border dimensions
    12.         lngBorderWidth = (.picScroll.Width - .picScroll.ScaleWidth)
    13.         lngBorderHeight = (.picScroll.Height - .picScroll.ScaleHeight)
    14.         ' Initialize to maximum displayable client size
    15.         lngWidth = .ScaleWidth - lngBorderWidth
    16.         lngHeight = .ScaleHeight - lngBorderHeight
    17.         ' Identify which scrollbars are necessary
    18.         Do
    19.             blnAdjusting = False
    20.             If .picClient.Width > lngWidth And Not blnScrollX Then
    21.                 lngHeight = lngHeight - .scrHorizontal.Height
    22.                 blnScrollX = True
    23.                 blnAdjusting = True
    24.             End If
    25.             If .picClient.Height > lngHeight And Not blnScrollY Then
    26.                 lngWidth = lngWidth - .scrVertical.Width
    27.                 blnScrollY = True
    28.                 blnAdjusting = True
    29.             End If
    30.         Loop While blnAdjusting
    31.         ' Add border size back in
    32.         lngWidth = lngWidth + lngBorderWidth
    33.         lngHeight = lngHeight + lngBorderHeight
    34.         If lngWidth < 0 Then lngWidth = 0
    35.         If lngHeight < 0 Then lngHeight = 0
    36.         ' Set client area
    37.         .picScroll.Move 0, .picHeader.Height, lngWidth, lngHeight
    38.         ' Set scrollbars
    39.         .scrHorizontal.Move 0, .ScaleHeight - .scrHorizontal.Height, lngWidth, .scrHorizontal.Height
    40.         If .scrHorizontal.Value <> 0 Then .scrHorizontal.Value = 0
    41.         If blnScrollX Then
    42.             .scrHorizontal.Max = .picClient.ScaleWidth - .picScroll.ScaleWidth
    43.             .scrHorizontal.LargeChange = .picScroll.ScaleWidth
    44.         End If
    45.         If .scrHorizontal.Visible <> blnScrollX Then .scrHorizontal.Visible = blnScrollX
    46.         .scrVertical.Move .ScaleWidth - .scrVertical.Width, 0, .scrVertical.Width, lngHeight
    47.         If .scrVertical.Value <> 0 Then .scrVertical.Value = 0
    48.         If blnScrollY Then
    49.             .scrVertical.Max = .picClient.ScaleHeight - .picScroll.ScaleHeight
    50.             If .picScroll.ScaleHeight < Screen.TwipsPerPixelY Then
    51.                 .scrVertical.LargeChange = Screen.TwipsPerPixelY
    52.             Else
    53.                 .scrVertical.LargeChange = .picScroll.ScaleHeight
    54.             End If
    55.         End If
    56.         If .scrVertical.Visible <> blnScrollY Then .scrVertical.Visible = blnScrollY
    57.     End With
    58. End Sub
    59.  
    60. Private Sub scrHorizontal_Change()
    61.     ScrollX Me.scrHorizontal.Value
    62. End Sub
    63.  
    64. Private Sub scrHorizontal_GotFocus()
    65.     Me.picScroll.SetFocus
    66. End Sub
    67.  
    68. Private Sub scrHorizontal_Scroll()
    69.     ScrollX Me.scrHorizontal.Value
    70. End Sub
    71.  
    72. Private Sub scrVertical_Change()
    73.     ScrollY Me.scrVertical.Value
    74. End Sub
    75.  
    76. Private Sub scrVertical_GotFocus()
    77.     Me.picScroll.SetFocus
    78. End Sub
    79.  
    80. Private Sub scrVertical_Scroll()
    81.     ScrollY Me.scrVertical.Value
    82. End Sub
    83.  
    84. Private Sub ScrollX(plngScrollX As Long)
    85.     Me.picClient.Left = -plngScrollX
    86. End Sub
    87.  
    88. Private Sub ScrollY(plngScrollY As Long)
    89.     Me.picClient.Top = -plngScrollY
    90. End Sub
    Those focus events are to suppress the tendency of VB6 scrollbars to do this annoying flashing thing when they have the focus.

  7. #7
    Fanatic Member
    Join Date
    Apr 2021
    Posts
    616

    Re: Scroll bar in picturebox

    It's either picClient or picScroll...too early for me to think too hard about it, but just try one and if it doesn't work try another (and if still not, try other elements that it could be...in fact, did you really need to post asking?)

  8. #8

  9. #9
    Fanatic Member
    Join Date
    Apr 2021
    Posts
    616

    Re: Scroll bar in picturebox

    Quote Originally Posted by MartinLiss View Post
    You guys may not have noticed, but this thread is 14 years old.
    I noticed, but the OP didn't...still needed a response :-)

  10. #10
    Fanatic Member HackerVlad's Avatar
    Join Date
    Nov 2023
    Posts
    674

    Re: Scroll bar in picturebox

    Quote Originally Posted by MartinLiss View Post
    You guys may not have noticed, but this thread is 14 years old.
    What difference does it make how old the topic is if there is still no correct solution on one picturebox. I also really need to make scrollbars for the image. But I want to use one picture box. How can this be done? How do I move the image left and right if I have only one picturebox on my form?

  11. #11
    Fanatic Member HackerVlad's Avatar
    Join Date
    Nov 2023
    Posts
    674

    Re: Scroll bar in picturebox

    Since no one has ever created a technology for scrolling an image based on only one picturebox, I had to do it myself today.

    I want to draw attention to the fact that there are much fewer lines of code in my implementation. My version is much simpler than yours.

    Code:
    Option Explicit
    Dim cPic As StdPicture
    ' We will scroll the picture here from 0 to -319 (320 in total)
    
    Private Sub DrawFit(ByVal hDC As Long, ByVal cPic As IPicture, ByVal NewX As Long)
        Dim lsw As Long
        Dim lsh As Long
        
        lsw = CLng(((cPic.Width / 2540) * 1440) / Screen.TwipsPerPixelX)
        lsh = CLng(((cPic.Height / 2540) * 1440) / Screen.TwipsPerPixelY)
        
        cPic.Render hDC, -NewX, 0, lsw, lsh, 0, cPic.Height, cPic.Width, -cPic.Height, ByVal 0&
    End Sub
    
    Private Sub Paint_Picture()
        Dim NewValueX As Long
        
        NewValueX = HScroll1.Value
        Me.Caption = NewValueX
        
        Picture1.Cls
        DrawFit Picture1.hDC, cPic, NewValueX
    End Sub
    
    Private Sub HScroll1_Change()
        Paint_Picture
    End Sub
    
    Private Sub HScroll1_Scroll()
        Paint_Picture
    End Sub
    
    Private Sub Form_Load()
        Set cPic = LoadPicture(App.Path & "\123456789.jpg")
        HScroll1.Max = Picture1.ScaleWidth
        Paint_Picture
    End Sub
    Attached Images Attached Images  
    Attached Files Attached Files

  12. #12
    Fanatic Member HackerVlad's Avatar
    Join Date
    Nov 2023
    Posts
    674

    Re: Scroll bar in picturebox

    Here is another example using the scrollbar from the legendary user The Trick. I have placed the scroll bar here on the form, behind the picture. However, given that the control itself is transparent, it could be installed even inside the picturebox.
    Attached Images Attached Images  
    Attached Files Attached Files

  13. #13
    Fanatic Member HackerVlad's Avatar
    Join Date
    Nov 2023
    Posts
    674

    Re: Scroll bar in picturebox

    New design project
    Attached Files Attached Files

  14. #14
    Fanatic Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    589

    Re: Scroll bar in picturebox

    Quote Originally Posted by HackerVlad View Post
    New design project
    Very good control, it would be possible to capture the mouse wheel in the control

  15. #15
    Fanatic Member HackerVlad's Avatar
    Join Date
    Nov 2023
    Posts
    674

    Re: Scroll bar in picturebox

    Quote Originally Posted by yokesee View Post
    Very good control, it would be possible to capture the mouse wheel in the control
    Yes, you can do it if you try.

  16. #16
    Fanatic Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    589

    Re: Scroll bar in picturebox

    What way do you recommend?
    I have tried clsTrickSubclass2 from Trick but the IDE crashes

  17. #17
    Fanatic Member HackerVlad's Avatar
    Join Date
    Nov 2023
    Posts
    674

    Re: Scroll bar in picturebox

    Quote Originally Posted by yokesee View Post
    What way do you recommend?
    I have tried clsTrickSubclass2 from Trick but the IDE crashes
    Is a Subclass needed for this?

    I personally would not recommend anything, as I have no idea how to do this. But for some reason I was sure that it was possible to do without subclassing...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width