例1:运行一个可执行文件 文件
[-p[2t{.S`4g1CZ0Extern.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 50051Testing软件测试网vE w:as7C;x
'它让我的机器在办公室里狂响不止!(不停的响暂不知如何中止,最好在虚拟机中试)51Testing软件测试网
]
@5e"Qt1c l7} ~h
51Testing软件测试网h1J"U{'I%TqA
例3:QTP运行后锁键盘和鼠标。
r.N*|8?.l s CN!t/|0Extern.Declare micLong, "BlockInput", "user32.dll", "BlockInput", micLong 51Testing软件测试网_ t+R6W6i*A%t2U0r
' Use the following statement to lock keyboard and mouse input at the
-~6kKbJP9EG0Extern.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.Declare和Extern.<已声明的方法>方法更改记事本窗口的标题。
]^/|(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 t1Rl
'声明SetWindowText方法51Testing软件测试网,P!}f8~E K
Extern.Declare micLong, "SetWindowText", "user32.dll", "SetWindowTextA", micHwnd, micString51Testing软件测试网fKg/k Ws.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`$a0endif
1c OgZT0'更改记事本窗口的标题
aAV-Vn0\ly?^ }X0res= Extern.SetWindowText(hwnd, "hukai")51Testing软件测试网r.P/_Tu6Tvs
51Testing软件测试网H0h(S%dO V
以下示例使用GetPrivateProfileString从外部INI文件中检索信息,并将该信息用于测试。注意,如果使用micByRef标志,则表示为输出参数。
8rSU5W5{0D~j0Extern.Declare micInteger,"GetPrivateProfileStringA", "kernel32.dll","GetPrivateProfileStringA", micString, micString, micString, micString+micByRef, micInteger, micString51Testing软件测试网W,TP!?1`3S/YS%f
Dim key, i, key2
!z$|$h3m1Sj0key= String(32, "-")
_w/h5yp0i =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\v0extern.DeclaremicLong,"AttachThreadInput","user32.dll","AttachThreadInput",micLong,
ht_p{Y0micLong,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"
V9qA2eS0O5EH0extern.DeclaremicLong,"GetCursor","user32.dll","GetCursor"
(la#U0PhMs7A0functionget_cursor()51Testing软件测试网m5b
b7Vwl/X
hwnd= extern.GetForegroundWindow()
8bUbe7}%?'sst#U0pid= extern.GetWindowThreadProcessId(hWnd, NULL)51Testing软件测试网
mT"dz1?*]7r|&]d
thread_id=extern.GetCurrentThreadId()
9U]$`8C:f_;x[-P0extern.AttachThreadInputpid,thread_id,True
z*qG5YVvv0get_cursor=extern.GetCursor()51Testing软件测试网
f,}
^,x [.i:m4]b4K
extern.AttachThreadInputpid,thread_id,False
LF!j2vP5Kj0endfunction51Testing软件测试网?4z*m|@GzC*DR8L.P
Msgbox get_cursor()
#vQ0G9o)Ux%p0
1I'x%\"i/bq4HE2D$K1F0FindWindow函数用于查找窗体 51Testing软件测试网:i~DEt(K8@],lpy
函数原型51Testing软件测试网 L6h$^W
L
HWND FindWindow(51Testing软件测试网3DLUn P8MgO
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 M jW}9]