如果你能在你的应用程序中添加一些热键,就可以加快用户的操作速度,特别是对那些熟练的操作人员,他们特别欢迎快捷键的操作方式。在不少大型应用软件中,用户可以通过使用ctrl+alt+f5之类的组合键来方便地进行功能切换和处理。那么,我们在用powerbuilder开发应用程序时,怎样在其中实现需要的热键功能呢?下面笔者就根据自身的经验,介绍两种实用的方法。
第一种方法:
该方法可以实现:无论何时,只要用户按下热键,都将触发窗口中的事件。
1.声明 api的外部函数
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参数用于指定使用本热键的窗口句柄,id参数用于指定一个惟一的id,fsmodifiers参数指明辅助键值(alt、ctrl、shift等),vk参数指明虚拟键的ascii码。
2.对常量赋初值
public:
constant integer mod-alt = 1
constant integer mod-control = 2
constant integer mod-shift = 4
3.利用代码在系统中注册要使用的热键
//在窗口的open事件中
long ll-rc
string ls-str
ls-str = "my atom id"
atomid = globaladdatom(ls-str) //得到惟一的id,保证不和其他应用程序发生冲突
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的外部函数
function long sendmessagea(long lhwnd,uint uimsg,long lwmsg,long lwparam) library ′user32.dll′
2.对常量赋初值
public:
constant long wm-sethotkey=50//设置热键信息值
constant long hk-myhotkey=1648 //热键参数值
constant long sc-hotkey=61776//pb中的热键信息
其中,hk-myhotkey不是固定的,它根据用户的需要而定。它的具体值的确定方法是:高8位字节与低8位字节组成16位字节,然后将它换算成十进制数,即得到所需的hk-myhotkey值。高8位字节值为一些辅助键(control、alt、shift等),低8位字节为使用键的ascii码。如果我们要使用ctrl+alt+a作为热键,则a=65,转换成十六进制为41,ctrl+alt=2+4=6,转换成十六进制仍然是6,两则组合即为641,再重新转换回十进制得到1601;同样,如果我们用ctrl+alt+f1作为热键,f1=112,可以得到hk-myhotkey值应为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
。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器