问题
I'm making a program in vb6 that requires me to be able to scroll a certain area of the screen to the right. It consists solely of lines and picture boxes, is there a way to only scroll that area? Thanks!
回答1:
Here is a simple example illustrating what you requested. The key is that the scrollable area must be a container control hosting the controls you wish to scroll.
Option Explicit
Private oldPos As Integer
Private Sub Form_Load()
HScroll1.Min = 0
HScroll1.Max = 1000
HScroll1.SmallChange = Screen.TwipsPerPixelX * 10
HScroll1.LargeChange = HScroll1.SmallChange
End Sub
Private Sub HScroll1_Change()
ScrollPictureBox
End Sub
Private Sub HScroll1_Scroll()
ScrollPictureBox
End Sub
Private Sub ScrollPictureBox()
Dim c As Control
For Each c In Me.Controls
If c.Container.Name = "Picture1" And Not TypeOf c Is HScrollBar Then
c.Left = c.Left - (oldPos - HScroll1.Value)
End If
Next
oldPos = HScroll1.Value
End Sub
In this code, Picture1 is a PictureBox (the scrollable area) containing HScroll1 (a horizontal scrollbar) and the other controls you wish to scroll.
回答2:
You can take advantage from the fact that some visual controls can act as container of other visual controls.
Just an example:
In the VBIDE, place a Frame
over a VB Form. Then - inside this Frame
place a PictureBox
. Pay attention that the PictureBox
shall be fully contained inside this Frame
.
Now, if you drag the Frame around the Form, you will see that the PictureBox inside is moving together, while keeping the position inside the container Frame, i.e. it will keep the original top & left coordinates relative to the container control.
To find out which visual controls have this capability, simply retry the test. You will see, for example, that a Label
can't act as a container.
That said, you need following:
- one visual control (preferably a PictureBox) which act as container (the viewport)
- one visual control (preferably a PictureBox) which act as scrollable area
- a
HScrollBar
(and, optionally, aVScrollBar
) to scroll the view-able area
Now, inside the second PictureBox
(the scrollable area) you can place your controls, the Lines
and PictureBoxes
you mentioned in your question.
Why is a PictureBox
preferable? Because you can profit from the ScaleMode
property, set it to 3-Pixel
and use pixel-exact scrolling. With Frames
you can't do that, you are limited to just Twips
.
By using a contained control you have two advantages:
- you can visually place and reposition the controls ypu need inside the IDE
- you need to scroll just only one control - all other hosted controls will move together
The boring thing you must code is the synchronization of the container with the ScrollBars.
But luckily, as VB6 has been going a long way, you will find enough cut-and-paste code examples of such a task, one of which is on VBForums: Scroll bar in picturebox
Some final notes:
PictureBoxes
in VB6 are constrained to a maximum size of 16,383 x 16,383 pixels. If your scrollable area should be bigger, you may implement a kind of custom "infinite scroller", and manage the position of your controls by grouping them, and you will need some additional coding.
ScrollBars
in VB6 can range from a minimum value of -32,768 to a maximum value of 32,767. If you need more, you will end up with some other additional coding tasks.
If you stick to Twips
, you can have a bigger logical area available - for example: until 245,745 with the typical 15 TwipsPerPixel - but you can't use such a big value with ScrollBars.
来源:https://stackoverflow.com/questions/44181917/how-can-i-make-a-certain-part-of-my-form-scrollable