打印本文 打印本文 关闭窗口 关闭窗口
PB调用外部程序及判断其完成的方法
作者:武汉SEO闵涛  文章来源:敏韬网  点击数777  更新时间:2009/4/24 21:42:56  文章录入:mintao  责任编辑:mintao
P>在PB中常常需要运行一些外部的程序或命令,并等待其执行完成后,才接下来运行剩余的代码。我们可以有两种方法:

  先定义全局外部函数:

Function long ShellExecuteA (long hwnd, string lpOperation ,String lpFile, String lpParameters, String lpDirectory, Long nShowCmd) Library "shell32.dll"

Function long FindWindowA (String lpClassName , String lpWindowName ) Library "user32.dll"

Function boolean IsWindow (Long hwnd ) Library "user32.dll"

  第一种方式用Run() 函数,可在窗口上建立按扭,clicked事件中包含如下Script:

ulong ll_handle

int li_loop

SetPointer(HourGlass!)

//最小化执行xxx.bat

run("xxx.bat", Minimized!)

//循环到窗口打开,根据程序执行打开所需的时间设定li_loop的循环次数,可预留长一些。

for li_loop= 1 to 10000

ll_handle = FindWindowA("tty","xxx")

yield()

if ll_handle $#@60;$#@62; 0 then

exit

end if

next

//一直循环到窗口关闭

Do While isWindow(ll_handle)

Yield()

Loop

//应用执行完成

messagebox(‘ok’, ‘执行完成!’)

  这种方法的缺点是不能隐藏外部应用程序窗口,只能最小化。

  第二种方式用API函数,可以隐藏应用程序的窗口,但是调用bat批处理命令时需要先建立一个PIF文件指定执行完成后关闭窗口,否则窗口不会自行关闭。可在窗口上建立按扭,clicked事件中包含如下Script:

uint lu_return

ulong ll_handle

int li_loop

string ls_Path

SetPointer(HourGlass!)

lu_return = ShellExecutea(handle(parent), "open", "xxx.pif", "", ls_path, 0)

//最后一个参数改为 4,可以显示执行情况

if lu_return $#@62; 32 then

for li_loop= 1 to 10000

ll_handle = FindWindowA("tty","xxx")

yield()

if ll_handle $#@60;$#@62; 0 then

exit

end if

next

//一直循环到窗口关闭

Do While isWindow(lu_handle)

Yield()

Loop

//应用执行完成

Messag x("ok", "执行完成!")

Else

//error

messagebox("错误", "调用外部应用程序不成功,请检查应用程序路径!")

end if

打印本文 打印本文 关闭窗口 关闭窗口