VFP调用API实现代码模拟鼠标.键盘动作

*--VFP调用API实现代码模拟鼠标.键盘动作

 

*--模拟键盘协作,如 Ctrl+V
Declare keybd_event In Win32API Short bVk,Short bScan,Integer dwFlags, Integer
deExtraInfo
keybd_event(17, 0, 0, 0)
keybd_event(
Asc
('V'), 0, 0, 0)
keybd_event(
Asc
('V'), 0, 2, 0)
keybd_event(17, 0, 2, 0)


*或
#Define
VK_CONTROL 0x11
#
Define
KEYEVENTF_KEYUP 0x02
Declare Long keybd_event In "user32" Integer bVk , Integer bScan, Long dwFlags, Long
dwExtraInfo
Clear Typeahead
keybd_event(VK_CONTROL, 0, 0, 0)            
  && 模拟按下 Ctrl 键
keybd_event(Asc('V'), 0, 0 ,0)                
&& 模拟按下 V 键
keybd_event(Asc('V'), 0, KEYEVENTF_KEYUP, 0)
  && 松开 V 键
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0) && 松开 Ctrl 键


* KEYEVENTF_KEYUP = &H2 ' Release key
* VK_RETURN = &HD 'enter
* VK_SHIFT = &H10 'Shift
* VK_CONTROL = &H11 'Ctrl
* VK_CAPITAL = &H14 'capslock
* VK_ESCAPE = &H1B 'escape


*--模拟鼠标动作,如 单击左键
MOUSEEVENTF_LEFTDOWN=0x2
MOUSEEVENTF_LEFTUP=0x4
MOUSEEVENTF_MIDDLEDOWN=0x20
MOUSEEVENTF_MIDDLEUP=0x40
MOUSEEVENTF_MOVE=0x1
MOUSEEVENTF_ABSOLUTE=0x8000
MOUSEEVENTF_RIGHTDOWN=0x8
MOUSEEVENTF_RIGHTUP=0x10
Declare mouse_event In Win32API Integer dwFlags,Integer dx,Integer dy,Integer cButtons,Integer dwExtraInfo
*在鼠标坐标系统中,不管什么分辨率屏幕在水平和垂直方向上均匀分割成65535×65535个单元

=mouse_event(MOUSEEVENTF_ABSOLUTE+MOUSEEVENTF_MOVE,10000,10000,0,0)
&&移动鼠标到绝对位置10000,10000
=mouse_event(MOUSEEVENTF_LEFTDOWN+MOUSEEVENTF_LEFTUP,0,0,0,0)      
&&单击左键(按下再松开的组合)
*=mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0)
*=mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0)

'--以下为VB模拟鼠标的说明及例子
'-------------------------------------------'
' 模拟鼠标的左键单击和右键单击'
'-------------------------------------------'
'程序说明:'
'本例采用API函数实现模拟的鼠标事件,程序运行后会'
'产生十分有趣的效果。也来试一试。'
'本例中只使用了相对鼠标坐标,我们也可以使用绝对'
'鼠标坐标来试一试。'
'-------------------------------------------'

'【VB声明】'
' 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)'

'【说明】'
' 模拟一次鼠标事件'

'【备注】'
' 进行相对运动的时候,由SystemParametersInfo函数规定的系统鼠标轨迹速度会应用于鼠标运行的速度'

'【参数表】'
' dwFlags -------- Long,下述标志的一个组合'
' MOUSEEVENTF_ABSOLUTE'
' dx和dy指定鼠标坐标系统中的一个绝对位置。在鼠标坐标系统中,屏幕在水平和垂直方向上均匀分割成65535×65535个单元 -'
' MOUSEEVENTF_MOVE 移动鼠标'
' MOUSEEVENTF_LEFTDOWN 模拟鼠标左键按下'
' MOUSEEVENTF_LEFTUP 模拟鼠标左键抬起'
' MOUSEEVENTF_RIGHTDOWN 模拟鼠标右键按下'
' MOUSEEVENTF_RIGHTUP 模拟鼠标右键抬起'
' MOUSEEVENTF_MIDDLEDOWN 模拟鼠标中键按下'
' MOUSEEVENTF_MIDDLEUP 模拟鼠标中键抬起'
' dx ------------- Long,根据是否指定了MOUSEEVENTF_ABSOLUTE标志,指定水平方向的绝对位置或相对运动'

' dy ------------- Long,根据是否指定了MOUSEEVENTF_ABSOLUTE标志,指定垂直方向的绝对位置或相对运动'

' cButtons ------- Long,未使用'

' dwExtraInfo ---- Long,通常未用的一个值。用GetMessageExtraInfo函数可取得这个值。可用的值取决于特定的驱动程序'
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 _
)

'Option_Tag标示选择了哪一种模拟事件'
Dim Option_Tag As Integer
'OnTest标示是否处于模拟状态,以便我们停止模拟'
Dim OnTest As Boolean
'对API变量的定义'
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

'控制 模拟的开始与结束'
Private Sub Command1_Click()

'如果不处于模拟状态'
If OnTest = False Then
Command1.Caption = "快停下来吧"
Timer1.Enabled = True
OnTest = True
'如果处于模拟状态'
Else
Command1.Caption = "试一试"
Timer1.Enabled = False
OnTest = False
End If
End Sub

'窗体加载时一些变量需要设置'
Private Sub Form_Load()
Option_Tag = 1
Timer1.Enabled = False
OnTest = False
End Sub

'选择了模拟“鼠标左键单击事件”'
Private Sub Option1_Click()
Option_Tag = 1 '左键单击事件
Option2.Value = False
Option3.Value = False
End Sub

'选择了模拟“鼠标右键单击事件”'
Private Sub Option2_Click()
Option_Tag = 2 '右键单击事件
Option1.Value = False
Option3.Value = False
End Sub

'选择了模拟“鼠标双击事件”'
Private Sub Option3_Click()
Option_Tag = 3 '双击事件
Option1.Value = False
Option2.Value = False
End Sub

'每隔一秒中模拟一次鼠标事件'
Private Sub Timer1_Timer()
If Option_Tag = 1 Then
'调用了mouse_event函数,其参数的设置见前面说明'
'如果同时要模拟两个鼠标事件,可以用 Or 将两个参数连接'
'这里是 鼠标左键按下 和松开两个事件的组合即一次单击'
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
ElseIf Option_Tag = 2 Then
'模拟鼠标右键单击事件'
mouse_event MOUSEEVENTF_RIGHTDOWN Or MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
Else
'两次连续的鼠标左键单击事件 构成一次鼠标双击事件'
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End If
End Sub

 

相关内容推荐