VB中拖动控件代码
想要让鼠标在窗体内拖动一个控件(如标签)在窗体内任意移动,松开鼠标后控件停留在拖动后的位置。请问这段代码该怎么写???...
想要让鼠标在窗体内拖动一个控件(如标签)在窗体内任意移动,
松开鼠标后控件停留在拖动后的位置。
请问这段代码该怎么写??? 展开
松开鼠标后控件停留在拖动后的位置。
请问这段代码该怎么写??? 展开
2个回答
展开全部
我给你写了一段示例程序,拖动的控件时Label1。这个程序中,只有一个Label控件(name属性为Label1)和一个窗体。程序如下:
'定义一个布尔变量,用于标识控件是否可拖动
Dim canDrag As Boolean
Private Sub Form_Load()
'当窗体加载时,设置其值为false(即控件不可拖动)
canDrag = False
End Sub
Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'当按下鼠标时,控件可以拖动,但是这个动作不在这里执行
canDrag = True
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If canDrag Then '如果canDrag为真,这一动鼠标时就拖动控件
Label1.Left = Label1.Left + X
Label1.Top = Label1.Top + Y
End If
End Sub
Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'松开鼠标时,控件不可拖动
canDrag = False
End Sub
'定义一个布尔变量,用于标识控件是否可拖动
Dim canDrag As Boolean
Private Sub Form_Load()
'当窗体加载时,设置其值为false(即控件不可拖动)
canDrag = False
End Sub
Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'当按下鼠标时,控件可以拖动,但是这个动作不在这里执行
canDrag = True
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If canDrag Then '如果canDrag为真,这一动鼠标时就拖动控件
Label1.Left = Label1.Left + X
Label1.Top = Label1.Top + Y
End If
End Sub
Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'松开鼠标时,控件不可拖动
canDrag = False
End Sub
展开全部
在窗口放一个按钮,在复制下面代码运行测试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
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询