怎样在VB中设置一个可以拖动的控件?
2个回答
展开全部
Dim oldX!, oldY! '!是Single的缩写
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then '1就是鼠标左键
oldX = X
oldY = Y
End If
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Command1.Left = Command1.Left - oldX + X
Command1.Top = Command1.Top - oldY + Y
End If
End Sub
自己修改一下,把Command修改为你要拖动的控件。
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then '1就是鼠标左键
oldX = X
oldY = Y
End If
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Command1.Left = Command1.Left - oldX + X
Command1.Top = Command1.Top - oldY + Y
End If
End Sub
自己修改一下,把Command修改为你要拖动的控件。
展开全部
在窗口放一个按钮,在复制下面代码运行测试ok!!!
Dim xx, yy, ax, ay As Long
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
xx = X
yy = Y
End If
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
ax = Command1.Left + X
ay = Command1.Top + Y
Command1.Top = ay - yy
Command1.Left = ax - xx
End If
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then Print "你点击了按钮了"
If Button = 2 Then Print "你移动按钮了"
End Sub
Private Sub Form_Load()
Command1.Caption = "鼠标左键点击,鼠标右键移动"
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询