当我想对UI上的控件做一些特定的操作的时候,例如鼠标左右键点击,键盘输入字符串等,特别是在Object.Set和Object.Type没法使用的时候,DeviceReplay就显得非常有用。
DeviceReplay使用的前提是:
1. 应用程序窗口被激活。
对窗口应用程序: 可以使用Window("W").ActivatemicLeftBtn
对网页应用程序: 可以使用
hwnd = Browser("B").GetROProperty("hwnd")
Window("hwnd:="& hwnd ).ActivatemicLeftBtn
2. 目标控件为焦点。
WebEdit( "WE" ).object.focus 或者WebEdit( "WE" ).FireEvent "onfocusin"
使用以下方法可以判断当前哪些键是否按下:(Microsoft.VisualBasic.Devices.Keyboard)
Set Keyboard = DotNetFactory.CreateInstance("Microsoft.VisualBasic.Devices.Keyboard", "Microsoft.VisualBasic")
PrintCBool( Keyboard.AltKeyDown )
PrintCBool( Keyboard.CapsLock )
PrintCBool( Keyboard.CtrlKeyDown )
PrintCBool( Keyboard.NumLock )
PrintCBool( Keyboard.ScrollLock )
PrintCBool( Keyboard.ShiftKeyDown )
注意:使用DotNetFactory的时候要注意数据类型的转换
使用以下方法可以判断当前鼠标的位置:(System.Windows.Forms.Control)
Setctlr = DotNetFactory.CreateInstance("System.Windows.Forms.Control")
Fori = 1To10
Wait2
Print"1. X="& ctlr.MousePosition.X &"; Y="& ctlr.MousePosition.Y
Next
DeviceReplay有以下常用方法:
1。object.SendString( str )
SetdeviceReplay =CreateObject("Mercury.DeviceReplay")
SystemUtil.Run"notepad.exe", "", "","open"
' ** this line always identifies the notepad window.
Window("nativeclass:=Notepad","index:=0″).ActivatemicLeftBtn
deviceReplay.SendString("DeviceReplay")
SetdeviceReplay =Nothing
2。object.KeyDown( key )|object.KeyUp( key )
ConstVK_SHIFT = 42
ConstVK_RETURN = 28
SetdeviceReplay =CreateObject("Mercury.DeviceReplay")
SystemUtil.Run"notepad.exe", "", "","open"
Window("nativeclass:=Notepad","index:=0″).ActivatemicLeftBtn
' ** Typing uppercase
deviceReplay.KeyDown VK_SHIFT
deviceReplay.SendString("devicereplay")
deviceReplay.PressKey VK_RETURN
deviceReplay.KeyUp VK_SHIFT
' ** Typing in lower case
deviceReplay.SendString("devicereplay")
SetdeviceReplay =Nothing
3。object.PressKey( key )
ConstVK_O = 24 :ConstVK_F = 33
ConstVK_CONTROL = 29 :ConstVK_ESCAPE = 1 :ConstVK_MENU = 56
SetdeviceReplay =CreateObject("Mercury.DeviceReplay")
SystemUtil.Run"notepad.exe", "", "","open"
Window("nativeclass:=Notepad","index:=0″).ActivatemicLeftBtn
Wait1
' ** Opening the menu Alt + F + O
deviceReplay.PressKey VK_MENU
deviceReplay.PressKey VK_F
deviceReplay.PressKey VK_O
Wait2
' ** Closing the menu
deviceReplay.PressKey VK_ESCAPE
deviceReplay.SendString"Open menu was closed."
SetdeviceReplay =Nothing
4.object.PressNKeys( key, N )
用于模拟连续多次按某个键。
5.object.DragAndDrop( dragX, dragY, dropX, dropY, Button )
LEFT_MOUSE_BUTTON = 0
MIDDLE_MOUSE_BUTTON = 1
RIGHT_MOUSE_BUTTON = 2
等同于object.MouseDown( x, y, Button ),object.MouseDown( x, y ),object.MouseUp( x, y, Button )的组合
6.object.MouseClick( x, y, Button )
7.object.MouseDblClick( x, y, Button )
8。object.SetSynchronizationTimeout(object,nSyncTimeout,is_sec)