在PB中判断应用程序是否打开
  应用程序通常需要查询其他运行于主机上打开的应用程序,并要判断是否另一个应用程序需要先被打开。
可以用两种方法完成这项工作。

1。findwindow()API函数:
申明如下:
//WIN31
FUNCTION uint findwindow(string szclass,string szname) library "user.exe"
//win32
FUNCTION uint findwindow(string szclass,string szname) library "user32.dll"
上面的两个参数可以是NULL,这时函数要匹配所有的类和标题,如果能找到的话,函数将返回窗口的窗口句柄,反之,返回0。
如:若要查看windows calculator应用程序是否已打开,代码如下:
unit hwnd
hwnd=g_app.externals.uf_finwindo(scicalc","calculator")
if hwnd=0 then
run("calc")
else
g_app.externals.uf_setfocus(hwnd)
end if
注意:setfocus(),可以把已打开的窗口带到前景。setfocus()函数要求一个windows句柄为参数,在pb内定义如下:
//win31:
function uint setfocus(uint hwnd) library "user.exe"
//win32
function uint setfocus(uint hwnd) library "user32.dll"
若要知道dos提示窗口是否已打开,需要把类名参数设置为tty,若要查找特定的dos提示符窗口,可以查找名 为ms-dos prompt的窗口,大小写,空格,以及 字符必须完全与该窗口的匹配。
2。只用于win16
两个函数:getmodulehandle()和getmoduleusage()
getmodulehandle()将返回lpszmodulename指定的模块的句柄,否则将返回null.
int getmoduleusage()(hinstance hinst);
getmoduleusage()将返回由hinst指定的模块使用数目,windows操作系统每当应用程序加载时都将梯增模块的引用数,每当程序关闭时,递减该数。申明如下:
//win31
function uint getmodulehandle(string szmoudlename)library "krnl386.exe"
function uint getmoduleusage(uinthend) library "krnl386.exe"
如检查已经运行的word是否存在的代码如下;
uin t uimodule
integer nusage
uimodule=getmodulehandle("winword.exe")
nusage=getmoduleusage(uimodule)
if nusage=0 then
run(winword.exe")
end if
利用handle()函数的可选参数可以查看同一个可执行文件的另一个pb应用程序的存在,如下:
if handle(this,true)>0 then
//already running
halt close
end if