Private ctrName As String '控件名称
Private isClick As Boolean '鼠标点击状态
'注:如果已知点击目标控件的父控件,ctrParent变量可以不要。
Private WithEvents ctrParent As Control '父控件
Private Sub ControlAMouseDown(sender As Object, e As MouseEventArgs) _
Handles Button1.MouseDown
isClick = (e.Button = MouseButtons.Left _
Or e.Button = MouseButtons.Right) '左键或右键按下
If isClick Then
Dim ctr As Control = CType(sender, Control) '转换Object为控件类型
ctrName = ctr.Name '获取控件名称
ctrParent = ctr.Parent '获取控件的父控件
End If
End Sub
'增加这个父控件事件,是为了正确判别鼠标弹起时是否已进入指定目标
Private Sub ParentMouseMove(sender As Object, e As EventArgs) _
Handles ctrParent.MouseMove '如果已取消ctrParent变量,改为相应的父控件
If isClick Then isClick = False '点击状态关闭
End Sub
Private Sub ControlBMouseUp(sender As Object, e As EventArgs) _
Handles Button2.MouseEnter
If isClick Then '如果点击状态为打开
Dim ctr As Control = CType(sender, Control) '转换Object为控件类型
MsgBox(ctrName & " | " & ctr.Name) '弹出消息显示结果
End If
End Sub
用 sender.tostring就可以了
但是有一个问题,象你说的那样,弹起鼠标的地方,捕获不到mouseup事件,因为在鼠标按下状态时,它进入第二个控件时,不会引起控件焦点改变,应该是正常状态进入到第二个控件才行。