一些控制鼠标的例子

1.模拟鼠标击键过程
'声明:
Option Explicit
Private Declare Sub mouse_event Lib "user32" ( ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long )
'对变量的定义
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
'这里是 鼠标左键按下 和松开两个事件的组合即一次单击
mouse_event MOUSEEVENTF_LEFTDOWN or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
'模拟鼠标右键单击事件
mouse_event MOUSEEVENTF_RIGHTDOWN or MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
'两次连续的鼠标左键单击事件 构成一次鼠标双击事件
mouse_event MOUSEEVENTF_LEFTDOWN or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTDOWN or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
2.模拟鼠标显示.隐藏
隐藏/显示鼠标.
Public Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
'forml中函数如下
'隐藏鼠标(需要事件击活,比如窗体事件等)
ShowCursor False
'显示鼠标(需要事件击活,比如窗体事件等)
ShowCursor True
3.定位鼠标,使之不能移动
定位鼠标。
Type rect
sbleft As Long
sbtop As Long
sbright As Long
sbbottom As Long
End Type
Public Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long

'鼠标定位
Private Sub Form_Load()
'定位鼠标
Dim x As Long, y As Long
Dim newrect As rect
x& = Screen.TwipsPerPixelX
y& = Screen.TwipsPerPixelY

With newrect '鼠标只能在500,500-500,500这个范围内移动,如果四个数一样也可以说锁定鼠标了.如果加在记时器里的话就移动不了啦.
.sbleft = 500
.sbtop = 500
.sbright = 500
.sbbottom = 500
End With

ClipCursor newrect
如果鼠标被锁定,不能恢复怎么办 不用担心.看如下代码.
'使鼠标恢复(设定一个事件.才好击活这个代码.)
Dim newrect As rect
With newrect '这样鼠标又可以在0,0-屏幕的最右角,屏幕的最右下脚移动了
.sbleft = 0
.sbtop = 0
.sbright = Screen.Width / Screen.TwipsPerPixelX
.sbbottom = Screen.Height / Screen.TwipsPerPixelY
End With

ClipCursor newrect
End Sub
4.移动鼠标到某某点
移动鼠标.
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
窗口函数如下:
SetCursorPos 0, 0 '鼠标移动到0,0点
5.鼠标坐标
鼠标坐标.
Type POINTAPI
x As Long
y As Long
End Type
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
'鼠标坐标:
在窗口中设立一记时器。一显示框。
Dim z As POINTAPI
GetCursorPos z
Label1.Caption = "x: " & z.x & " y: " & z.y '设定一个显示点label1.
6.鼠标键数
'鼠标键数.
Public Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
'鼠标键数
Dim mousebtn As Long
mousebtn = GetSystemMetrics(43)
Label1.Caption = "你的鼠标是 " & mousebtn & " 键鼠标!" '设定一个显示点label1.
以上也可以使用其他方式模拟.

未经允许不得转载:Sleep's Blog » 一些控制鼠标的例子

相关文章

评论 (0)

8 + 3 =

*