微软Windows应用编程接口(API)为开发Windows应用程序提供了很多构建好的模块。它提供各种操作方法,比如获取鼠标坐标,窗口句柄,颜色等。QTP支持调用动态链接库内定义的这些方法,但是由于VBScript的限制,只有有限的一部分API可以在QTP中使用。
本章使用的API的更多细节和信息可以从MSDN或者Visual Studio的API Viewer工具查询获得。
Extern对象
QTP提供一个Extern功能对象用来声明和调用API。
语法
Extern.Declare (RetType, MethodName, LibName, Alias [, ArgType(s)]) |
更多细节可参考QTP用户手册。
VB API定义语法
Private Declare Function GetForegroundWindow Lib "user32.dll" () As Long |
对以上API,我们需要使用QTP的Declare方法确定使用的合适参数:
RetType = micLong(函数返回类型)
MethodName = "GetForegroudWindow" (我们可以使用任何其他名字,但是比较好的做法是使用实际API的名字)
LibName = "user32.dll" (如果不是使用Windows系统自带的DLL,那么文件名必须使用绝对路径,例如"C:\MyApp\Lib\mylib.dll")
Alias = "" 或者"GetForegroudWindow"(如果MethodName跟Alias相同,则Alias可以为空值。
ArgType(s) = 本例中不需要传入参数)。
QTP API定义
Extern.Declare micLong,"GetForegroundWindow","user32.dll","GetForegroundWindow" |
以下用一些用例来演示使用API解决通常遇到的问题。
问题 17-1.如何判断当前桌面最上面的为浏览器窗口
'声明GetForeGroundWindow API extern.Declare micLong,"GetForegroundWindow","user32.dll","GetForegroundWindow"
'获取最前面窗口的句柄 hwnd = extern.GetForegroundWindow()
'判断是否有包含此句柄的浏览器窗口 isBrowser = Browser("hwnd:=" &hwnd).Exist() If isBrowser then Msgbox "The top most window is a browser" End if |
问题 17-2.如何获得Windows的环境变量 (注意不是QTP环境变量)
'变量声明 Dim s_EnvValue
'声明 API "GetEnvironmentVariable" Extern.Declare micLong,"GetEnvironmentVariable","kernel32.dll","GetEnvironmentVariableA", _ micString,micString+micByRef,micLong
'获取全局变量 "TEMP" 值 Extern.GetEnvironmentVariable "TEMP",s_EnvValue,255 '会得到临时目录路径 MsgBoxs_EnvValue |
问题 17-3.如何使用Windwos API选中Listbox里的选项(item)
' 声明API Extern.Declare micLong,"SendMessage","user32.dll","SendMessageA",micLong,micLong,micLong,micLong
' 设置Listbox选项item的消息 Const LB_SETSEL = &H185
'方法:根据index选中复选框 Function CheckListBox(hwnd, index) extern.SendMessagehwnd, LB_SETSEL, True, index end function
'方法:根据index取消选中复选框 Function UnCheckListBox(hwnd, index) extern.SendMessagehwnd, LB_SETSEL, False, index end function |
问题 17-4.如何取到一个文本框的背景色 (当验证必填项跟可选项底色不同时可用到)
'声明需要的 API Extern.Declare micLong,"GetPixel","gdi32","GetPixel",micLong,micLong,micLong Extern.Declare micLong,"GetWindowDC","user32","GetWindowDC",micLong Extern.Declare micLong,"ReleaseDC","user32","ReleaseDC",micLong,micLong Extern.Declare micLong,"GetDC","user32","GetDC",micLong Extern.Declare micLong,"SetForegroundWindow","user32","SetForegroundWindow",micLong
Dim hDCSource Dim hWndSource Dim backColor '取得控件的句柄 hWndSource = Window("Window").WinEdit("MandatoryField1").GetROProperty("hwnd")
'将窗口置于最上面,因为GetPixel方法只能针对可见像素使用 extern.SetForegroundWindowhWndSource
'取得设备上下文句柄 hDCSource = Clng(Extern.GetDC(hWndSource))
'取得相关控件的像素(1,1)点的背景色 backColor = Clng(Extern.GetPixel(hDCSource, Clng(1),Clng(1))) MsgBoxbackColor
'释放设备上下文句柄 Extern.ReleaseDChWndSource, hDCSource |
问题 17-5.如何使用Windows API模拟键盘操作
'声明API:键盘事件keybd_event extern.Declare micVoid,"keybd_event","user32" ,"keybd_event", _ micbyte,micbyte,miclong,miclon '声明API:虚拟按键码映射MapVirtualKey extern.Declare micLong,"MapVirtualKey","user32","MapVirtualKeyA", _ micLong, micLong
Const KEYEVENTF_EXTENDEDKEY = &H1 Const KEYEVENTF_KEYUP = &H2 Const KEYEVENTF_KEYDOWN = &H0
Sub KeyDown(KeyAscii) keyCode = extern.MapVirtualKey(KeyAscii, 0) '触发按键按下事件 extern.keybd_eventKeyAscii, keyCode, KEYEVENTF_KEYDOWN, 0 End Sub
Sub KeyUp(KeyAscii) keyCode = extern.MapVirtualKey(KeyAscii, 0) '触发按键抬起事件 extern.keybd_eventKeyAscii, keyCode, KEYEVENTF_KEYUP, 0 End Sub
Sub KeyPress(KeyAscii) KeyDownKeyAscii KeyUpKeyAscii End Sub |
对“计算器”使用以上代码
'声明键值常量 Const vbKey1 = 49 Const vbKey2 = 50 ConstvbKeyAdd = 107 ConstvbKeyReturn = 13
SystemUtil.Run"calc.exe" Window("title:=Calculator").Activate Call KeyPress(vbKey1) Call KeyPress(vbKeyAdd) Call KeyPress(vbKey2) Call KeyPress(vbKeyReturn) |
使用以上代码模拟CTRL+ALT+S
'声明键值常量 ConstvbKeyControl = 17 ConstvbKeyAlt = 18 ConstvbKeyS = 83
Call KeyDown(vbKeyControl) Call KeyDown(vbKeyAlt) Call KeyDown(vbKeyS) Call KeyUp(vbKeyS) Call KeyUp(vbKeyAlt) Call KeyUp(vbKeyControl) |
问题 17-4.如何取到一个文本框的背景色 (当验证必填项跟可选项底色不同时可用到)
'声明需要的 API Extern.Declare micLong,"GetPixel","gdi32","GetPixel",micLong,micLong,micLong Extern.Declare micLong,"GetWindowDC","user32","GetWindowDC",micLong Extern.Declare micLong,"ReleaseDC","user32","ReleaseDC",micLong,micLong Extern.Declare micLong,"GetDC","user32","GetDC",micLong Extern.Declare micLong,"SetForegroundWindow","user32","SetForegroundWindow",micLong
Dim hDCSource Dim hWndSource Dim backColor '取得控件的句柄 hWndSource = Window("Window").WinEdit("MandatoryField1").GetROProperty("hwnd")
'将窗口置于最上面,因为GetPixel方法只能针对可见像素使用 extern.SetForegroundWindowhWndSource
'取得设备上下文句柄 hDCSource = Clng(Extern.GetDC(hWndSource))
'取得相关控件的像素(1,1)点的背景色 backColor = Clng(Extern.GetPixel(hDCSource, Clng(1),Clng(1))) MsgBoxbackColor
'释放设备上下文句柄 Extern.ReleaseDChWndSource, hDCSource |
问题 17-5.如何使用Windows API模拟键盘操作
'声明API:键盘事件keybd_event extern.Declare micVoid,"keybd_event","user32" ,"keybd_event", _ micbyte,micbyte,miclong,miclon '声明API:虚拟按键码映射MapVirtualKey extern.Declare micLong,"MapVirtualKey","user32","MapVirtualKeyA", _ micLong, micLong
Const KEYEVENTF_EXTENDEDKEY = &H1 Const KEYEVENTF_KEYUP = &H2 Const KEYEVENTF_KEYDOWN = &H0
Sub KeyDown(KeyAscii) keyCode = extern.MapVirtualKey(KeyAscii, 0) '触发按键按下事件 extern.keybd_eventKeyAscii, keyCode, KEYEVENTF_KEYDOWN, 0 End Sub
Sub KeyUp(KeyAscii) keyCode = extern.MapVirtualKey(KeyAscii, 0) '触发按键抬起事件 extern.keybd_eventKeyAscii, keyCode, KEYEVENTF_KEYUP, 0 End Sub
Sub KeyPress(KeyAscii) KeyDownKeyAscii KeyUpKeyAscii End Sub |
对“计算器”使用以上代码
'声明键值常量 Const vbKey1 = 49 Const vbKey2 = 50 ConstvbKeyAdd = 107 ConstvbKeyReturn = 13
SystemUtil.Run"calc.exe" Window("title:=Calculator").Activate Call KeyPress(vbKey1) Call KeyPress(vbKeyAdd) Call KeyPress(vbKey2) Call KeyPress(vbKeyReturn) |
使用以上代码模拟CTRL+ALT+S
'声明键值常量 ConstvbKeyControl = 17 ConstvbKeyAlt = 18 ConstvbKeyS = 83
Call KeyDown(vbKeyControl) Call KeyDown(vbKeyAlt) Call KeyDown(vbKeyS) Call KeyUp(vbKeyS) Call KeyUp(vbKeyAlt) Call KeyUp(vbKeyControl) |