请问哪位老师能帮助我,捕获鼠标滚
用API函数GetCursorPos可以获得鼠标指针的当前位置。所以以下代码可以做到监测鼠标的滚动方向和滚动事件发生。本文参考万方数据电子出版社”出版的“趣味编程”Ⅱ (2001。5)的“计算鼠标行程”一文。 作相应改动。
Option Explicit
Private Type PointAPI
X As Long
Y As Long
End Type
Dim MousePos As PointAPI
Private Declare Function GetCursorPos Lib "user32" _
(lpPoint As PointAPI) As Long
Dim OldX A...全部
用API函数GetCursorPos可以获得鼠标指针的当前位置。所以以下代码可以做到监测鼠标的滚动方向和滚动事件发生。本文参考万方数据电子出版社”出版的“趣味编程”Ⅱ (2001。5)的“计算鼠标行程”一文。
作相应改动。
Option Explicit
Private Type PointAPI
X As Long
Y As Long
End Type
Dim MousePos As PointAPI
Private Declare Function GetCursorPos Lib "user32" _
(lpPoint As PointAPI) As Long
Dim OldX As Long
Dim OldY As Long
Dim NewX As Long
Dim NewY As Long
Dim UnitValue As Long
Dim UnitName As String
Dim FormatStr As String
Private X0 As Long
Private Y0 As Long
Const FormatStr1 = "000000。
00"
Const FormatStr2 = "0000。0000"
Const hudu_jiaodu = 57。29577951
Private Sub Command1_Click()
End
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1。
FontBold = True
End Sub
Private Sub Form_Load()
UnitValue = 1440
UnitName = "英寸"
FormatStr = FormatStr1
Timer1。
Enabled = True
GetCursorPos MousePos
OldX = MousePos。X * Screen。TwipsPerPixelX
OldY = MousePos。
Y * Screen。TwipsPerPixelY
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1。
FontBold = False
End Sub
Private Sub Timer1_Timer()
GetCursorPos MousePos
NewX = MousePos。
X * Screen。TwipsPerPixelX
NewY = MousePos。Y * Screen。TwipsPerPixelY
X0 = NewX - OldX
Y0 = NewY - OldY
If X0 <> 0 Or Y0 <> 0 Then
Text1 = "鼠标滚动"
Text1。
BackColor = vbRed
Else
Text1 = "鼠标静止"
Text1。BackColor = vbGreen
End If
If Y0 0 Then
Text3 = "东偏北"
Text4 = 90 - Int(hudu_jiaodu * Atn(-X0 / Y0) * 10 + 0。
501) / 10
ElseIf Atn(-X0 / Y0) 0 Then
If Atn(-X0 / Y0) = 0 Then
Text3 = "南"
Text4 = ""
ElseIf Atn(-X0 / Y0) 0 Then
Text3 = "西偏南"
Text4 = 90 - Int(hudu_jiaodu * Atn(-X0 / Y0) * 10 + 0。
501) / 10
End If
ElseIf Y0 = 0 Then
If X0 > 0 Then
Text3 = "东"
Text4 = ""
ElseIf X0 = 0 Then
Text3 = "原地"
Text4 = ""
ElseIf X0 < 0 Then
Text3 = "西"
Text4 = ""
End If
End If
OldX = NewX
OldY = NewY
End Sub
其中Timer属性Interval设得小灵敏度高。
收起