西安海星系统集成中心 田俊国
---- 对象被右键单击就弹出一个功能菜单是windows应用程序一项不可缺少的功能。在powerbuilder应用开发中,可在需弹出右键菜单对象的rbuttondown事件中调用该菜单的popmenu( )方法实现这一功能。但多数情况下,应用中有多处要用到不同的弹出式菜单,如一一用这种方法就需要做大量的工作。能不能做一个通用的菜单,可以用任何对象的rbuttondown事件激发弹出,并在弹出前动态改变弹出菜单项的内容,当点击菜单项时又可执行被右击对象的相应用户事件?由于这一技术最大的特点是当函数创建成功后,可在任何应用的任何对象中随心所欲的使用,随时随地弹出自己想要显示的菜单项,并执行相应的自己需要的功能脚本,但目前各种资料上鲜有系统、成型的介绍。笔者抱着一种探索的态度,作了一番仔细深入的研究,并小有收获,愿和大家共同探讨。
一、 创建弹出菜单m_popup
创建一个菜单m_popup,定义菜单条m_main,其下有十五个菜单项,分别命名为m_item1,m_item2,……m_item15,各项的显示文本(text)分别为item1,item2,… …item15;
为m_popup菜单定义一个powerobject类型的instance变量:anyobject
脚本为:
powerobject anyobject
给m_item1,m_item2,… …m_item15各菜单项的clicked事件下分别输入脚本:
anyobject.triggerevent("ue_item1")
//m_item1的clicked事件
anyobject.triggerevent("ue_item2")
… …
anyobject.triggerevent("ue_item15")
---- 4、 定义几个菜单函数:
---- 1) setmenuitem(string itemstring),返回值为null。
---- 参数itemstring是由多个子串组成的。各个子串间用“”间隔,每个子串为一个菜单项的显示文本(text)。
---- 该函数功能是把itemstring分解为多个子串,并把子串赋给相应菜单项的text。脚本如下:
int itempos,itemorder=1,i
string currentitem
if len(itemstring)=0 then return
itempos=pos(itemstring,"")
do while itempos<>0
// itempos为间隔符""的位置
currentitem=left(itemstring,itempos -1)
//取出子串
itemstring=mid(itemstring, itempos+1)
this.m_main.item[itemorder].text=currentitem
itempos=pos(itemstring,"")
itemorder++
loop
this.m_main.item[itemorder].text=itemstring
for i=1 to itemorder
this.m_main.item[i].visible=true
this.m_main.item[i].enabled=true
end if
for i=itemorder+1 to 15
this.m_main.item[i].visible=false
next
2)setitemdisable(integer itemorder) 返回值null。
该函数把第itemorder菜单项置灰(disable)。
脚本如下:
if itemorder<1 or itemorder>15 then return
this.m_main.item[itemorder].enabled=false
3)popupmenu(integer x, integer y) 返回值null。
该函数弹出菜单条m_main。
脚本如下:
this.m_main.popmenu(x,y)
二、 rbuttondown事件激发弹出菜单m_popup
---- 至此,我们就可以在窗口中任意对象如datawindow、 picture、 singlelineedit、 listbox、 picturelistbox、 dropdownpicturelistbox、 multilineedit、 listview、 treeview等的rbuttondown事件中写脚本调用m_popup的函数来实现右键单击弹出一个菜单。下面以数据窗口dw_1为例,在其rbuttondown事件下写入脚本,使得右击dw_1 可弹出菜单:刷新/插入/删除/修改。步骤如下:
1、事先给窗口定义一个m_popup 型instance变量om_1:
m_popup om_1
2、dw_1的rbuttondown事件脚本:
if not isvalid(om_1) then
om_1= create m_popup
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 注册表 操作系统 服务器 应用服务器