如何突破滚动条最大值是32767的限制(vb6)

Hi:

I'm using VB6 and am trying to compensate for the scrollbar max of 32767.  Here's what I'm trying to do.  I have a picture control within a picture control.  Let's call the outside picture control picContainer and the picture control within it picChild.  It's set up so that at design time if I move picContainer, picChild also moves.  My form is being used to view images and picChild is what contains the images.  When an image is loaded, picChild is resized to the size of the picture.  When picChild is larger than picContainer, picContainer contains a vertical scrollbar so that I can move picChild up and down to see the image (I'm just worrying about the vertical right now, forget about the horizontal).  I set my scrollbar (called fsb, for flat scroll bar) settings as follows:

fsb.Max = picChild.Height - picContainer.Height
fsb.SmallChange = Abs(fsb1.Max " 16) + 1
fsb.LargeChange = Abs(fsb1.Max " 4) + 1  

The program works perfectly except for when (picChild.Height - picContainer.Height) > 32767, the largest value the scrollbar max property can handle.  Attempting to set the max beyond that value of course causes an error to be generated.

Any ideas as to how to compensate for this max of 32767 so that I can still scroll to see the entire picture?  My goal is to be able to scroll up and down to see the entire picture but not scroll beyond the picture dimensions (I don't want the bottom of the picture scrolling up passed the top of the screen).

Looking forward to your replies.

Thanks.
ksm

What you can do is leave your scrollbar min at 0 and the max at 100.  Then you can use the current value of the scrollbar to compute the percentage of how far up/down the inner picturebox control should be moved.

Here is an example.  Create a new project and a TextBox, CommandButton, HorizontalScrollBar, VerticalScrollBar, PictureBox and a Windows Common Dialog Control.   Draw an additional PictureBox inside the first PictureBox.  Position the TextBox and CommandButton next to each other across the top of your form.  Make the first PictureBox take up the rest of the form.  The second PictureBox, HorizontalScrollBar and the VerticalScrollBar will be positioned at runtime via code.

Regards,

Idle_Mind

Option Explicit

Private horzMax As Single
Private vertMax As Single

Private Sub Form_Load()
    Text1.Locked = True
    Text1.Text = ""
    Command1.Caption = "Select Image"

    With Picture2
        .Move 0, 0
        .AutoSize = True
        .Width = Picture1.Width
        .Height = Picture1.Height
    End With

    With HScroll1
        .Top = Picture1.Top + Picture1.Height
        .Left = Picture1.Left
        .Width = Picture1.Width
        .Min = 0
        .Max = 100
        .SmallChange = 4
        .LargeChange = 10
    End With

    With VScroll1
        .Top = Picture1.Top
        .Left = Picture1.Left + Picture1.Width
        .Height = Picture1.Height
        .Min = 0
        .Max = 100
        .SmallChange = 4
        .LargeChange = 10
    End With
End Sub

Private Sub Command1_Click()
    On Error GoTo cancelled
    
    CommonDialog1.CancelError = True
    CommonDialog1.DialogTitle = "Select an Image to View"
    CommonDialog1.Filter = "Image Files (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif|JPG Files (*.jpg)|*.jpg|BMP Files (*.bmp)|*.bmp|GIF Files (*.gif)|*.gif"
    CommonDialog1.ShowOpen
    Text1.Text = CommonDialog1.fileName
    Call loadImage(CommonDialog1.fileName)
cancelled:
End Sub

Private Sub loadImage(ByVal fileName As String)
    On Error Resume Next
    
    If Dir(fileName) <> "" Then
        With Picture2
            .Move 0, 0
            .Picture = LoadPicture(fileName)
        End With

        horzMax = Picture2.Width - Picture1.Width
        With HScroll1
            .Value = 0
            If horzMax < 0 Then
                .Max = 0
                .Visible = False ' Optional
            Else
                .Max = 100
                .Visible = True ' Optional
            End If
        End With
                
        vertMax = Picture2.Height - Picture1.Height
        With VScroll1
            .Value = 0
            If vertMax < 0 Then
                .Max = 0
                .Visible = False ' Optional
            Else
                .Max = 100
                .Visible = True ' Optional
            End If
        End With
    End If
End Sub

Private Sub HScroll1_Change()
    If HScroll1.Max > 0 Then
        Picture2.Left = -(HScroll1.Value / HScroll1.Max) * horzMax
    End If
End Sub

Private Sub VScroll1_Change()
    If VScroll1.Max > 0 Then
        Picture2.Top = -(VScroll1.Value / VScroll1.Max) * vertMax
    End If
End Sub
posted @ 2009-08-27 10:27  大漠银狐  阅读(1393)  评论(0编辑  收藏  举报