问题 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) |
原文转自:http://www.uml.org.cn/Test/201307084.asp