对于 MouseMove 事件,button 指出了鼠标按钮的全部状态─ 不象在 MouseDown 和 MouseUp 事件中那样仅仅指出哪个按钮触发事件。可以设置所有位或某些位,或者不设置任何位,所以还能够提供附加信息。这与 MouseDown 和 MouseUp 过程中一个事件只能设置一位形成对照。
如果检测 MouseMove 是否等于 001(十进制 1),则意味着检测在移动鼠标时是否只按住左按钮。如果还有其它按钮与左按钮一同被按住,则下列代码不显示任何信息:
Private Sub Form_MouseMove (Button As Integer, _
Shift As Integer, X As Single, Y As Single)
If Button = 1 Then Print "You're pressing _
only the left button."
End Sub
为了检测是否按下某个特定按钮,应使用 And 操作符。下列代码显示每个被按下的按钮的信息,而不管是否还按下其它按钮:
Private Sub Form_MouseMove (Button As Integer, _
Shift As Integer, X As Single, Y As Single)
If Button And 1 Then Print "You're pressing _
the left button."
If Button And 2 Then Print "You're pressing _
the right button."
End Sub
同时按下两个按钮后,在窗体上将显示两条信息。MouseMove 事件能够识别多个按钮状态。
在大多数情况下,为了将已按下的按钮隔离开,应使用 MouseMove 事件。
在上一个程序的基础上可用 If…Then…Else 语句判断左、右按钮中的哪一个(或者全部)被按下。以下示例检测三种按钮状态(按下左按钮、按下右按钮、同时按下两个按钮)并显示相应信息。
请将下列代码添加到窗体的 MouseMove 事件中:
Private Sub Form_MouseMove(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Print "You're pressing the left button."
ElseIf Button = 2 Then
Print "You're pressing the right button."
ElseIf Button = 3 Then
Print "You're pressing both buttons."
End If
End Sub
也可用 And 操作符与 Select Case 语句判断 button 和 shift 状态。And 操作符与 Select Case 语句结合可隔离三按钮鼠标可能具有的按钮状态并显示相应信息。
在窗体声明部分创建名为 ButtonTest 的变量:
Dim ButtonTest as Integer
将下列代码添加到窗体的 MouseMove 事件中:
Private Sub Form_MouseMove(Button As Integer, _ Shift As Integer, X As Single, Y As Single) ButtonTest = Button And 7 Select Case ButtonTest Case 1 '或vbLeftButtonPrint "You're pressing the left button."Case 2 '或vbRightButtonPrint "You're pressing the right button."Case 4 '或vbMiddleButtonPrint "You're pressing the middle button."Case 7Print "You're pressing all three buttons."End SelectEnd Sub