Form上添加一个PictureBox和两个TextBox控件,属性为默认,代码如下:
Option Explicit
Dim X1 As Long '记录鼠标弹起状态经过Picture1获得其X值
Dim L As Long '记录Picture1的Left值
Dim W As Long '记录Text2的宽度
Dim Apsty As Long '设定左边与右边限定距离
Private Sub Form_Load()
Me.AutoRedraw = True: Me.ScaleMode = 3
Picture1.Move Me.ScaleWidth / 2, 0, 8, Me.ScaleHeight
Picture1.BorderStyle = 0
Text1.Move 0, 0, Picture1.Left, Me.ScaleHeight '初始化Text1的位置
Text2.Move Picture1.Left + Picture1.Width, Me.ScaleWidth - Text2.Left, Me.ScaleHeight '初始化Text2的位置
L = Picture1.Left '获取Picture1的Left值到变量L
W = Text2.Width '获取Text2的宽度到变量W
Apsty = IIf(Me.ScaleMode <> vbPixels, 50 * 15, 50)
End Sub
Private Sub Form_Resize()
If Me.ScaleWidth < L + W Then
Picture1.Move IIf(Me.ScaleWidth - W < Apsty, Apsty, Me.ScaleWidth - W)
Text1.Move 0, 0, Picture1.Left
Text2.Move Picture1.Left + Picture1.Width, 0, Me.ScaleWidth - (Picture1.Left + Picture1.Width)
Else
Text2.Move Picture1.Left + Picture1.Width, 0, Me.ScaleWidth - (Picture1.Left + Picture1.Width)
End If
Picture1.Height = Me.ScaleHeight
Text1.Height = Me.ScaleHeight
Text2.Height = Me.ScaleHeight
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Picture1.MousePointer = 9
If Button = 1 Then
If Picture1.Left + X - X1 > Apsty And Picture1.Left + X - X1 < Me.ScaleWidth - Apsty Then
Picture1.Move Picture1.Left + (X - X1), 0
End If
Else
X1 = X
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Move 0, 0, Picture1.Left
Text2.Move Picture1.Left + Picture1.Width, 0, Me.ScaleWidth - (Picture1.Left + Picture1.Width)
L = Picture1.Left
W = Text2.Width
End Sub