1. 如何使pb窗口总在最上层 
通过setwindowpos函数吧窗口的显示层次修改为hwnd_topmost,就可以使指定窗口永远不会被其它窗口覆盖,该函数声明为: 
function long setwindowpos(long hwnd, long ord, long x, long y, long 
dx, long dy, long uflag) library “user32.dll” 
参数1为要顶层显示的窗口句柄,参数2指定显示的层次,参数7为附加选项,其余 
参数指定窗口位置和大小,均可忽略。在窗口的open或activate事件中加入如下 
函数调用: 
setwindowpos(handle(this),-1,0,0,0,0,3) 
参数2取-1表示在最顶层显示窗口,取1表示在最底层显示;最后一个参数若取1, 
表示窗口大小保持不变,取2表示保持位置不变,因此,取3(=1+2)表示大小和 
位置均保持不变,取0表示将窗口的大小和位置改变为指定值。 
 
2. 在pb中如何获得光盘盘符 
通过getdrivetype函数可以获取驱动器(如:软驱、硬盘、光驱、网络映像驱动 
器等)的信息,该函数声明为: 
function unit getdrivetypea(string drive) library “kernel32.dll” 
参数为一个盘符(如“c:”),返回值:1表示未知,2表示软驱,3表示本地硬盘 
,4表示网络驱动器,5表示光驱。因此如下代码可以获得光盘的盘符: 
for i=asc(‘d’) to asc(‘z’) 
//列举所有可能的cdrom的驱动器 
if getdrivetypea(char(i)+”:”) = 5 then 
//若找到cdrom 
messagebox(“cdrom”,char(i)+”:”) 
//显示光盘盘符 
exit //退出循环 
end if 
next 
 
3. 在pb中如何获取目录信息 
(1) 获取当前目录。通过getcurrentdirectory函数可以获取当前目录,该函数 
声明为: 
function ulong getcurrentdirectory(ulong buflen,ref string dir) 
library “kernel32.dll” 
参数2为接受当前目录的字符缓冲区,前面必须加ref表示地址引用;参数1用来指 
定字符缓冲区的长度。调用过程为: 
string curdir 
curdir=space(256) 
//为字符缓冲区开辟内存空间 
getcurrentdirectory(256,curdir) 
messagebox(“当前路径”,curdir) 
(2) 获取windows及系统目录。要用到getwindowsdirectory和getsystemdirec 
tory两个函数,须作如下声明: 
function uint getwindowsdirectorya(ref string dir,uint buflen) 
library kernel32.dll” 
function uint getsystemdirectorya(ref string dir,uint buflen) 
library "kernel32.dll” 
 
4. 在pb中如何注销当前用户、关闭计算机、重启计算机 
通过exitwindowsex函数可实现这三个功能,首先作如下声明: 
function long exitwindowsex(long uflag, long nouse) library "user32.dll” 
参数2保留不用,可取0;参数1取0可以注销当前用户,取1可以关闭计算机,取2 
可以重启计算机,其值再加4表示强制结束“未响应”的进程。 
 
5. 控制由run运行的程序(简称run程序) 
在pb程序设计中,可以用run()来运行一些程序。但run程序无法与pb主程序协调 
工作,若用户多次调用,就会启动run程序的多个实例,主程序退出时,run程序 
依然运行。可以用如下函数使它们协调工作: 
function ulong findwindowa(ulong classname, string windowname) 
library "user32.dll” 
function long setparent(long childwin, long parentwin) library "user32.dll” 
(1) 使run程序只运行一个实例 
handle = findwindowsa(nul,wtitle) 
//查找run程序是否已经运行,wtitle为run程序的窗口标题 
if handle > 0 then return 
//若已经在运行就返回 
run(“c:/luhan.chm”) 
//否则运行run程序 
(2) pb主程序退出时,run程序也关闭 
handle = findwindowa(nul,wtitle) 
setparent(handle,handle(w_main)) 
//使run程序窗口成为pb主程序的子窗口 
 
6. 映像网络驱动器 
若要在程序中把远程主机的资源映像到本地驱动器,可以用如下函数: 
function long wnetaddconnectiona(string path, string pwd, string drv) 
library “mpr.dll” 
如下代码可以把远程主机alexander上的共享文件夹my documents映像到本地的j 
盘: 
wnetaddconnectiona(“// alexander/ my documents”,””,”j:”) //参数2 
为访问口令 
它的作用相当于在dos提示符下执行:net use j: // alexander/ my documents 
 
7. 显示或隐藏windows的任务栏 
要显示或隐藏任务栏,首先要得到它的窗口句柄。任务栏是一个特殊的窗口,它 
的窗口类为:shell_traywnd,没有标题,故只能用findwindowex函数来取得它的 
句柄: 
function long findwindowex(long ph, long ch, ref string cn, ref 
string wn) library “user32.dll” 
function long showwindow(long hwnd, long ncmdshow) library “user32.dll” 
用showwindow来显示或隐藏窗口,其第二个参数为0表示隐藏,为5表示显示: 
handle = findwindowex(0,0,” shell_traywnd”,wn) //wn为空串 
showwindow(handle,0) //隐藏任务栏 
 
8. 如何将长文件名转换为短文件名 
通过getshortpathname函数可以把上文件名转换为8.3格式,其声明为: 
function long getshortpathnamea(string lf, ref string sf, long 
buflen) 
library “kernel32.dll” 
参数1为长文件名,参数2为保存短文件名的缓冲区,参数3为缓冲区长度。例如: 
 
getshortpathnamea(“c:/my document/powerbuilder编程实践.doc”,sf,256) 
/ 
//sf = spcace(256) 
 
9. 如何在pb中实现延时 
延时函数很有用,pb虽然没有提供,但可以通过wind32的sleep函数来扩展: 
function long sleep(long ms) library “kernel32.dll” 
调用:sleep(1000) //延时1秒 
 
10. 如何在pb中播放音乐 
pb没有提供任何多媒体函数,要播放音乐只能通过win32 api的playsound来实现 
: 
function long playsound(string filename, int mod, int flags) library 
“ winmm.dll” 
参数1为wav文件名,参数2必须取0,参数3取1表示后台播放,取8表示循环播放, 
因此取9(=1+8)表示在后台循环播放。