QTP调用WindowsAPI实例集

QTP调用WindowsAPI实例集

例1:运行一个可执行文件 文件
Extern.Declare micLong, "WinExec", "kernel32.dll", "WinExec", micString,micLong
Extern.WinExec "d:/1.exe ", 1

 

例2:Beep
Extern.Declare micLong, "Beep", "kernel32.dll", "Beep", micLong 
Extern.Beep 500
'它让我的机器在办公室里狂响不止!(不停的响暂不知如何中止,最好在虚拟机中试)

 

例3:QTP运行后锁键盘和鼠标。
Extern.Declare micLong, "BlockInput", "user32.dll", "BlockInput", micLong 
' Use the following statement to lock keyboard and mouse input at the
Extern.BlockInput 1 ' beginning of the test:
wait(5)
' Use the following statement to unlock keyboard and mouse input at the
Extern.BlockInput 0  ' end of the test:

 

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

 

'声明 FindWindow 方法

Extern.Declare micHwnd, "FindWindow", "user32.dll", "FindWindowA", micString, micString

'声明 SetWindowText 方法

Extern.Declare micLong, "SetWindowText", "user32.dll", "SetWindowTextA", micHwnd, micString

'获取记事本窗口的 HWND

hwnd = Extern.FindWindow("Notepad", vbNullString)

if hwnd = 0 then

MsgBox "找不到记事本窗口"

end if

'更改记事本窗口的标题

res = Extern.SetWindowText(hwnd, "hukai")

 

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

Extern.Declare micInteger,"GetPrivateProfileStringA", "kernel32.dll","GetPrivateProfileStringA", micString, micString, micString, micString+micByRef, micInteger, micString

Dim key, i, key2

key = String(32, "-")

i = Extern.GetPrivateProfileStringA("WREnv","addons","xxx", key, 32, "wrun.ini")

key2 = Left(key,i)

msgbox key & ";" & key2 & ";" & CStr(i)

以下示例检查光标何时显示为沙漏。

extern.Declare micLong,"GetForegroundWindow","user32.dll","GetForegroundWindow"

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

micLong,micLong

extern.Declare micLong,"GetWindowThreadProcessId","user32.dll",

"GetWindowThreadProcessId",micLong,micLong

extern.Declare micLong,"GetCurrentThreadId","kernel32.dll","GetCurrentThreadId"

extern.Declare micLong,"GetCursor","user32.dll","GetCursor"

function get_cursor()

hwnd = extern.GetForegroundWindow()

pid = extern.GetWindowThreadProcessId(hWnd, NULL)

thread_id=extern.GetCurrentThreadId()

extern.AttachThreadInput pid,thread_id,True

get_cursor=extern.GetCursor()

extern.AttachThreadInput pid,thread_id,False

end function

Msgbox get_cursor()

 

FindWindow函数用于查找窗体   
函数原型
HWND FindWindow(
LPCTSTR lpClassName, // pointer to class name
LPCTSTR lpWindowName // pointer to window name
);

lpWindowName是要查找窗体的标题,即这里的“无标题 - 记事本”。
如果找到窗体,函数返回该窗体的句柄;如果找不到,函数返回空值或者零 。

句柄和窗口标题,若只知其中之一,""要用vbNullString表示

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值