2 回答

TA貢獻1886條經驗 獲得超2個贊
在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
添加回答
舉報