致力于测试团队建设和自动化测试开发,欢迎有兴趣者一起研究讨论

QTP调用WindowsAPI实例集

上一篇 / 下一篇  2008-09-18 10:43:34 / 个人分类:QTP

1:运行一个可执行文件 文件
[-p[2t{.S `4g1CZ0
Extern.Declare micLong, "WinExec", "kernel32.dll", "WinExec", micString,micLong51Testing软件测试网'L,c,q [QP
Extern.WinExec "d:\1.exe ", 1


S~ gnt]8b5W3C;L0

2:Beep51Testing软件测试网#r M |Q'E
Extern.Declare micLong, "Beep", "kernel32.dll", "Beep", micLong  
1Bj w7gl-o:g0Extern.Beep 500
51Testing软件测试网vE w:as7C;x
'它让我的机器在办公室里狂响不止!(不停的响暂不知如何中止,最好在虚拟机中试)51Testing软件测试网 ] @5e"Qt1cl7} ~h

 51Testing软件测试网h1J"U{'I%T qA

3:QTP运行后锁键盘和鼠标。
r.N*|8?.l sCN!t/|0
Extern.Declare micLong, "BlockInput", "user32.dll", "BlockInput", micLong 51Testing软件测试网_ t+R6W6i*A%t2U0r
' Use the following statement to lock keyboard and mouse input at the
-~6kKbJP9EG0
Extern.BlockInput 1' beginning of the test:
$W \L1B.@/SA3}'c0wait(5)
QQ6h7S#GbK Y0A0
' Use the following statement to unlock keyboard and mouse input at the51Testing软件测试网 fY.i;b wi
Extern.BlockInput 0 ' end of the test:51Testing软件测试网|g!tB F8Z1s

 

5Ik2\:}@`!Pc*B0

以下示例使用Extern.DeclareExtern.<已声明的方法>方法更改记事本窗口的标题。

]^/|(P f:X&jv0

 

g'`'q FZF(D@0

'声明FindWindow方法51Testing软件测试网0B aB%Q*k:e%A v%t~7Q

Extern.Declare micHwnd, "FindWindow", "user32.dll", "FindWindowA", micString, micString51Testing软件测试网y-Sdd t1R l

'声明SetWindowText方法51Testing软件测试网,P!}f8~E K

Extern.Declare micLong, "SetWindowText", "user32.dll", "SetWindowTextA", micHwnd, micString51Testing软件测试网fKg/k W s.A:hS.[9b

'获取记事本窗口的HWND51Testing软件测试网#M$u'|/f+JME

hwnd= Extern.FindWindow("Notepad", vbNullString)51Testing软件测试网QXHz|'f1A6@J

ifhwnd = 0 then51Testing软件测试网:F'?/{t#n/d3a

MsgBox "找不到记事本窗口"

[D'k8UR1[4`F`$a0

endif

1c OgZT0

'更改记事本窗口的标题

aAV-Vn0\ly?^ } X0

res= Extern.SetWindowText(hwnd, "hukai")51Testing软件测试网r.P/_Tu6Tvs

 51Testing软件测试网H0h(S%dO V

以下示例使用GetPrivateProfileString从外部INI文件中检索信息,并将该信息用于测试。注意,如果使用micByRef标志,则表示为输出参数。

8rSU5W5{0D~j0

Extern.Declare micInteger,"GetPrivateProfileStringA", "kernel32.dll","GetPrivateProfileStringA", micString, micString, micString, micString+micByRef, micInteger, micString51Testing软件测试网W,TP!?1`3S/Y S%f

Dim key, i, key2

!z$|$h3m1Sj0

key= String(32, "-")

_w/h5yp0

i =Extern.GetPrivateProfileStringA("WREnv","addons","xxx", key, 32, "wrun.ini")51Testing软件测试网^(A{{"k

key2 =Left(key,i)51Testing软件测试网'Nu(ah'^1~b

msgboxkey & ";" & key2 & ";" & CStr(i)

c5K$v ` Se0

以下示例检查光标何时显示为沙漏。51Testing软件测试网1M2y'm3t;y$Auy

extern.DeclaremicLong,"GetForegroundWindow","user32.dll","GetForegroundWindow"

i6Z(Y9qJ0[3MM \v0

extern.DeclaremicLong,"AttachThreadInput","user32.dll","AttachThreadInput",micLong,

ht_p{Y0

micLong,micLong51Testing软件测试网+MW/Q? F9`u$o4o-?

extern.DeclaremicLong,"GetWindowThreadProcessId","user32.dll",

kZ2l^2D0

"GetWindowThreadProcessId",micLong,micLong51Testing软件测试网$\B*HN|$g7{A

extern.DeclaremicLong,"GetCurrentThreadId","kernel32.dll","GetCurrentThreadId"

V9qA2e S0O5EH0

extern.DeclaremicLong,"GetCursor","user32.dll","GetCursor"

(la#U0P hMs7A0

functionget_cursor()51Testing软件测试网m5b b7Vwl/X

hwnd= extern.GetForegroundWindow()

8bU be7}%?'sst#U0

pid= extern.GetWindowThreadProcessId(hWnd, NULL)51Testing软件测试网 mT"dz1?*]7r|&] d

thread_id=extern.GetCurrentThreadId()

9U]$`8C:f_;x[-P0

extern.AttachThreadInputpid,thread_id,True

z*qG5YVvv0

get_cursor=extern.GetCursor()51Testing软件测试网 f,} ^,x [.i:m4]b4K

extern.AttachThreadInputpid,thread_id,False

LF!j2vP5Kj0

endfunction51Testing软件测试网?4z*m|@GzC*DR8L.P

Msgbox get_cursor()

#vQ0G9o)Ux%p0

 

1I'x%\"i/bq4H E2D$K1F0

FindWindow函数用于查找窗体   51Testing软件测试网:i~DEt(K8@],lpy
函数原型51Testing软件测试网 L6h$^W L
HWND FindWindow(51Testing软件测试网3DLUnP8MgO
LPCTSTR lpClassName,
// pointer to class name51Testing软件测试网oc5_z9k
LPCTSTR lpWindowName// pointer to window name51Testing软件测试网}/Dx0eYn:? L
);51Testing软件测试网;AWLu8K6RM
lpWindowName
是要查找窗体的标题,即这里的无标题-记事本
.O-k+X:GN2l0
如果找到窗体,函数返回该窗体的句柄;如果找不到,函数返回空值或者零51Testing软件测试网1L"l~2oqW;[

句柄和窗口标题,若只知其中之一,""要用vbNullString表示51Testing软件测试网0b)q MjW}9]


TAG: QTP

 

评分:0

我来说两句

日历

« 2024-01-11  
 123456
78910111213
14151617181920
21222324252627
28293031   

数据统计

  • 访问量: 89933
  • 日志数: 79
  • 图片数: 1
  • 建立时间: 2008-05-18
  • 更新时间: 2009-06-04

RSS订阅

Open Toolbar