我在一个Form上画了一个Label(就一个Label),然后实现将其进行拖动(代码如下),也就是将其MouseDown,MouseUp,MouseMove等方法进行扩展。我的想法是将扩展后的Label控件做成一个新控件,这个新控件封装了MouseDown,MouseUp,MouseMove等方法,拉来就可以用,而不需为每个Label编写相应的代码。
怎么实现?请高手指教,谢谢!
'定义鼠标移动前的位置
Dim tx As Single
Dim ty As Single
'定义差值
Dim mx As Integer
Dim my As IntegerPrivate Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'鼠标当前的位置
tx = X
ty = Y
End If
End Sub'鼠标松开
Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.MousePointer = vbDefault
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
mx = tx - X
my = ty - Y
With Label1
.Left = .Left - mx
.Top = .Top - my
End With
End If
End Sub
怎么实现?请高手指教,谢谢!
'定义鼠标移动前的位置
Dim tx As Single
Dim ty As Single
'定义差值
Dim mx As Integer
Dim my As IntegerPrivate Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'鼠标当前的位置
tx = X
ty = Y
End If
End Sub'鼠标松开
Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.MousePointer = vbDefault
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
mx = tx - X
my = ty - Y
With Label1
.Left = .Left - mx
.Top = .Top - my
End With
End If
End Sub
解决方案 »
- MSFlexGrid有固定行2行,有什么方法可以在固定行中实行自由合并,但除了固定行之外的行列永不合并?
- 连接Sybase8.0,请大家进来看一看。
- 请教,VB程序中 ACCESS新增数据的问题????
- 赚分,马上结...
- 请问一下,wsh (windows script host)在什么环境下开发最好
- vb中怎么调用系统的计算器!!在线等!
- 请问一周时间能学会vb数据库编程吗?请大虾指点
- 一个打包生成SETUP文件的问题,请高手指教,急!!!!!!!!!!!!
- 数字问题!!??
- 什么是垃圾,
- vb数据库编程中recordsettype中的三个属性dynaset,snapshot,table究竟有何区别?一定给分!
- 如何学习VB?
方法添加一个类
'定义鼠标移动前的位置
Public WithEvents Label1 As Label
Dim tx As Single
Dim ty As Single
'定义差值
Dim mx As Integer
Dim my As IntegerPrivate Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
'鼠标当前的位置
tx = X
ty = Y
End If
End Sub'鼠标松开
Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
mx = tx - X
my = ty - Y
With Label1
.Left = .Left - mx
.Top = .Top - my
End With
End If
End Sub在form中加入如下代码Private xx As New Class1
Private Sub Form_Load()
Set xx.Label1 = Label1
End Sub