在PB中實現(xiàn)熱鍵的方法
2024-07-21 02:10:12
供稿:網(wǎng)友
如果你能在你的應(yīng)用程序中添加一些熱鍵,就可以加快用戶的操作速度,特別是對那些熟練的操作人員,他們特別歡迎快捷鍵的操作方式。在不少大型應(yīng)用軟件中,用戶可以通過使用ctrl+alt+f5之類的組合鍵來方便地進行功能切換和處理。那么,我們在用powerbuilder開發(fā)應(yīng)用程序時,怎樣在其中實現(xiàn)需要的熱鍵功能呢?下面筆者就根據(jù)自身的經(jīng)驗,介紹兩種實用的方法。
第一種方法:
該方法可以實現(xiàn):無論何時,只要用戶按下熱鍵,都將觸發(fā)窗口中的事件。
1.聲明 api的外部函數(shù)
function integer globaladdatom(ref string lpstring) library "kernel32.dll" alias for "globaladdatoma"
function ulong registerhotkey(ulong hwnd,ulong id,ulong fsmodifiers,ulong vk) library "user32.dll"
//hwnd參數(shù)用于指定使用本熱鍵的窗口句柄,id參數(shù)用于指定一個惟一的id,fsmodifiers參數(shù)指明輔助鍵值(alt、ctrl、shift等),vk參數(shù)指明虛擬鍵的ascii碼。
2.對常量賦初值
public:
constant integer mod-alt = 1
constant integer mod-control = 2
constant integer mod-shift = 4
3.利用代碼在系統(tǒng)中注冊要使用的熱鍵
//在窗口的open事件中
long ll-rc
string ls-str
ls-str = "my atom id"
atomid = globaladdatom(ls-str) //得到惟一的id,保證不和其他應(yīng)用程序發(fā)生沖突
ll-rc = registerhotkey(handle(this), atomid, mod-alt + mod-control, 65)
// 65為‘a’,注冊的熱鍵為ctrl+alt+a
if ll-rc = 0 then
messagebox("錯誤","錯誤信息")
end if
4.編寫按下熱鍵時的處理程序
//在窗口的other事件中
if wparam = atomid then
//在這里編寫處理程序
end if
第二種方法:
1.聲明 api的外部函數(shù)
function long sendmessagea(long lhwnd,uint uimsg,long lwmsg,long lwparam) library ′user32.dll′
2.對常量賦初值
public:
constant long wm-sethotkey=50//設(shè)置熱鍵信息值
constant long hk-myhotkey=1648 //熱鍵參數(shù)值
constant long sc-hotkey=61776//pb中的熱鍵信息
其中,hk-myhotkey不是固定的,它根據(jù)用戶的需要而定。它的具體值的確定方法是:高8位字節(jié)與低8位字節(jié)組成16位字節(jié),然后將它換算成十進制數(shù),即得到所需的hk-myhotkey值。高8位字節(jié)值為一些輔助鍵(control、alt、shift等),低8位字節(jié)為使用鍵的ascii碼。如果我們要使用ctrl+alt+a作為熱鍵,則a=65,轉(zhuǎn)換成十六進制為41,ctrl+alt=2+4=6,轉(zhuǎn)換成十六進制仍然是6,兩則組合即為641,再重新轉(zhuǎn)換回十進制得到1601;同樣,如果我們用ctrl+alt+f1作為熱鍵,f1=112,可以得到hk-myhotkey值應(yīng)為1648。
3.利用代碼告訴窗口我們的熱鍵
//在窗口的open事件中
long ll-rc
ll-rc = sendmessagea(handle(this), wm-sethotkey, hk-myhotkey, 0)
if ll-rc <> 1 then
messagebox("錯誤","錯誤信息")
end if
4.編寫按下熱鍵時的處理程序
//在窗口的other事件中
if wparam = sc-hotkey then
//在這里編寫處理程序
end if
。
本文來源于網(wǎng)頁設(shè)計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。