如何获取Panel控件滚动条的取值范围,我发现拉到最下面时是负数
本帖最后由 gzw1000 于 2012-11-20 21:31:19 编辑 Panel里放了一个PictureBox想实现用鼠标拖动看图片,想要用代码控制滚动条的位置
Vb.net 里Panel没有VerticalScroll这个对象
[最优解释]
想让panel滚动,设置AutoScroll属性设置为 true,然后设置 Panel1.AutoScrollMinSize 为需要大小。panel就可以自动滚动了。要获取当前滚动位置Panel1.AutoScrollPosition 但这个位置是表示可滚动控件显示框的位置。负值表示远离原点方向滚动。详见msdn解释ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/fxref_system.windows.forms/html/933ee0ee-a537-edb0-653b-c0c8980b7aae.htm
下面给你一个 演示代码,需要一个按钮,一个panel ,一个picturebox 自己拖放到位置。
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize '设置picturebox为图像大小
PictureBox1.Location = New Point(0, 0)
Panel1.AutoScroll = True '启动滚动
End Sub
'加载图片
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dlg As New OpenFileDialog
With dlg
If .ShowDialog = Windows.Forms.DialogResult.OK Then
Try
PictureBox1.Image = Image.FromFile(.FileName) '加载图片
Panel1.AutoScrollMinSize = PictureBox1.Size '设置滚动范围
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End With
End Sub
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
End Sub
Private Sub Panel1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles Panel1.Scroll
Me.Text = String.Format("演示picturebox滚动及滚到当前位置:{0}", New Point(0, 0) - Panel1.AutoScrollPosition)
End Sub
End Class