用PB做一個通用右鍵菜單

用PB做一個通用右鍵菜單

(加入日期:2001-4-25)

保存文章至硬盤】【打印文章】【字體:

分享到: 0
西安海星系統集成中心 田俊國

---- 對象被右鍵單擊就彈出一個功能菜單是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
        end if
        //把菜單的anyobject指向被右擊的對象(dw_1)
om_1.anyobject=this
        om_1.setmenuitem("刷新|插入|刪除|修改")
//可在此調用om_1.setitemdisable(itemorder)函數disable某
   菜單項。
om_1.popupmenu(this.x+this.pointerx(),
    this.y+this.pointery())

3、 給dw_1定義用戶事件ue_item1、ue_item2、ue_item3
    及 ue_item4。
事件ue_item1腳本:
         dw_1.retrieve()
事件ue_item2腳本:
long newrow
newrow=dw_1.insertrow(0)
dw_1.scrolltorow(newrow)
事件ue_item3腳本:
dw_1.deleterow(0)
事件ue_item4腳本:
dw_1.update()

---- 這樣,只要用戶單擊dw_1彈出的右鍵菜單,就可完成對dw_1的插入、刪除、修改等功能。
三、 推廣爲全程函數

---- 如果應用中有許多地方要用此功能,我們可以把dw_1的rbuttondown事件腳本改造成一個全程函數。

---- 1、將變量om_1定義爲global變量:
---- m_popup om_1

---- 2、定義一個全程函數
---- pupmenu(powerobject sender,string itemstring,integer x,integer y) 返回值Null。其中參數sender爲被右擊的對象,itemstring爲彈出菜單的菜單項字符串,x、y爲菜單彈出的座標位置。

腳本如下:

if not isvalid(om_1) then
om_1= CREATE m_popup
end if
   om_1.anyobject=sender
   om_1.setmenuitem(itemstring)
   sender.triggerevent("ue_beforepop")
   //激活sender用戶事件。
 om_1.popupmenu(x,y)
     這樣,上述dw_1的rbuttondown事件腳本就可改寫爲:

     string items="刷新|插入|刪除|修改"
     popmenu(this, items ,this.x+this.pointerx(),
     this.y+this.pointery())
注意:在MDI 應用中,popmenu()函數需要改爲:
popmenu(this, items ,w_frame.pointerx(),w_frame.pointery())
其中w_frame爲MDI主窗口名。

---- 在函數popmenu中,又激活了被右擊對象的ue_beforepop用戶事件。如有必要,你可以給被右擊對象定義一個ue_beforepop事件,在該事件中可調用om_1.setitemdisable()函數來屏蔽某個菜單項。
---- 最後,別忘了在應用的close事件里加上下列語句,及時釋放系統內存。
---- if isvalid(om_1) then destroy om_1

---- 以上代碼在Windows98 和Powerbuilder6.5平臺上開發,並在多個大型系統中應用,效果很好。 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章