powerbuilder中API應用10則

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 )表示在后台循环播放。

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus