PowerBuilder學習API函數介紹收藏

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爲附加選項,其餘參數指定窗口位置和大小,均可忽略。在窗口的OpenActivate事件中加入如下函數調用:

 

  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及系統目錄。要用到GetWindowsDirectoryGetSystemDirec

 

 

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;參數10可以註銷當前用戶,取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程序是否已經運行,wtitleRun程序的窗口標題

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雖然沒有提供,但可以通過Wind32Sleep函數來擴展:

 

  Function Long Sleep(Long ms) Library kernel32.dll

 

  調用:Sleep(1000) //延時1

 

  10. 如何在PB中播放音樂

 

  PB沒有提供任何多媒體函數,要播放音樂只能通過Win32 APIPlaySound來實現

 

  Function Long PlaySound(String Filename, Int Mod, Int Flags) Library winmm.dll

 

  參數1wav文件名,參數2必須取0,參數31表示後臺播放,取8表示循環播放,因此取9=1+8)表示在後臺循環播放。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章