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

 

vfp9.0调用API制作照相软件实例 PUBLIC WM_CAP_DRIVER_DISCONNECT PUBLIC hwndc,WM_CAP_SAVEDIB,WM_CAP_FILE_SET_CAPTURE_FILEA,WM_CAP_SEQUENCE,WM_CAP_STOP DECLARE INTEGER capCreateCaptureWindowA IN "AVICAP32.DLL" STRING lpszWindowName ,INTEGER dwStyle , INTEGER x, INTEGER Y, INTEGER nWidth ,INTEGER nHeight,INTEGER ParentWin,INTEGER nId DECLARE INTEGER SendMessage IN "user32" INTEGER HWND, INTEGER wmsg,INTEGER wpar1, INTEGER wpar2 DECLARE INTEGER SendMessage IN "user32" AS SendMessageA INTEGER HWND, INTEGER wmsg,INTEGER wpar1, STRING wpar2 *!* 显示: WM_USER = 1024 WM_CAP_START = WM_USER WM_CAP_STOP = WM_CAP_START + 68 WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10 WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11 WM_CAP_SAVEDIB = WM_CAP_START + 25 WM_CAP_GRAB_FRAME = WM_CAP_START + 60 WM_CAP_SEQUENCE = WM_CAP_START + 62 WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20 WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+ 63 WM_CAP_SET_OVERLAY =WM_CAP_START+ 51 WM_CAP_SET_PREVIEW =WM_CAP_START+ 50 WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6 WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2 WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3 WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5 WM_CAP_SET_SCALE=WM_CAP_START+ 53 WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52 hWndC = capCreateCaptureWindowA('My Own Capture Window',1342177280,0,0,200,150,THISFORM.HWND ,0) &&显示大小 IF hWndC 0 SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0) SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0) SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0) SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0) SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0) SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0) SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0) SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0) * THISFORM.ACTIVATE ENDIF
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值