Use PB To make a general right-click menu

Source: Internet
Author: User

Right-click an object to bring up a function menu, which is an indispensable function of Windows applications. In PowerBuilder
In application development, you can call the Popmenu () method of the context menu object in the rbuttondown event. However, in most cases, different pop-up menus need to be used in multiple applications. For example, using this method one by one requires a lot of work. Can I make a general menu? I can use the rbuttondown event of any object to activate the pop-up and dynamically change the content of the pop-up menu item before the pop-up, when a menu item is clicked, can the corresponding user event of the right-clicked object be executed? The biggest feature of this technology is that after a function is created successfully, it can be used freely in any object of any application, and the menu items you want to display are displayed anytime and anywhere, and execute the functional scripts you need. However, there are few systematic and systematic introductions on various materials. With an attitude of exploration, I have made some in-depth research, and I am willing to discuss it with you.
1. Create the pop-up menu m_popup
Create a menu m_popup and define the menu entry m_main. There are 15 menu items under it, named m_item1, m_item2 ,...... M_item15. The displayed text of each item is item1, item2 ,... ... Item15; defines a Powerobject Instance variable for the m_popup menu: The Anyobject script is: Powerobject Anyobject to m_item1, m_item2 ,...... M_item15 input the Clicked event script for each menu item under the clicked event: Anyobject. triggerevent ("ue_item1") // m_item1 Clicked event
Anyobject. triggerevent ("ue_item2 ")
... ...
Anyobject. triggerevent ("ue_item15 ")
---- 4. Define several menu functions:
---- 1) setmenuitem (string itemstring). The return value is Null.
---- The Itemstring parameter is composed of multiple substrings. Each substring is separated by "|". Each substring is the text displayed for a menu item ).
---- This function breaks down itemstring into multiple substrings and assigns the substrings to the text of corresponding menu items. The script is as follows:
Int itempos, itemorder = 1, I string currentitem
If len (itemstring) = 0 then return itempos = pos (itemstring, "|") do while itempos <> 0
// Itempos is the location of the delimiter "|"
Currentitem = left (itemstring, itempos
-1) // retrieve the substring
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) returns Null. This function sets the itemorder menu item to "disable ). The script is as follows:
If itemorder <1 or itemorder> 15
Then
Return this. m_main.item [itemorder]. enabled = false
3) popupmenu (integer x, integer y) returns Null. M_main is displayed in the menu bar of this function. The script is as follows: this. m_main.popmenu (x, y)
Ii. rbuttondown event prompt menu m_popup
---- Now, right-click the m_popup function in the rbuttondown event of any objects in the window, such as DataWindow, Picture, SingleLineEdit, ListBox, PictureListBox, DropDownPictureListBox, MultiLineEdit, ListView, and TreeView. click to bring up a menu.
The following uses the data window dw_1 as an example to write a script under its rbuttondown event so that right-click dw_1 to bring up the menu:
Refresh/insert/delete/modify.
The procedure is as follows:

1. Define a rbuttondown event script for m_popup instance variable om_1: m_popup om_1 2 and dw_1 in advance for the window:
If not isvalid (om_1)
Then om_1 = CREATE m_popup
End if // point the menu anyobject to the right-clicked object (dw_1) om_1.anyobject = this

Om_1.setmenuitem ("Refresh | insert | Delete | modify") // you can call the om_1.setitemdisable (itemorder) function here.
Disable a menu item.
Om_1.popupmenu (this. x + this. pointerx (), this. y + this. pointery ())
3. Define user events ue_item1, ue_item2, ue_item3, and ue_item for dw_1
4. Event ue_item1 script:
Dw_1.retrieve ()
Event ue_item2 script:
Long newrow = dw_1.insertrow (0)
Dw_1.scrolltorow (newrow)
Event ue_item3 script:
Dw_1.deleterow (0)
Event ue_item4 script:
Dw_1.update ()
---- In this way, you can click the right-click menu popped up by dw_1 to complete the dw_1 insertion, deletion, modification, and other functions.
3. Promote full function
---- If this function is used in many applications, we can transform the rbuttondown event script of dw_1 into a full function.
---- 1. Define the variable om_1 as the global variable ---- m_popup om_1
---- 2. Define a full function ---- pupmenu (powerobject sender, string itemstring, integer x, integer y)
The return value is Null. Here, the sender parameter is the right-clicked object, itemstring is the menu item string of the pop-up menu, and x and y are the menu items
The output coordinate position. The script is as follows:
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") // activate the sender user event.
Om_1.popupmenu (x, y)
In this way, the rbuttondown event script of dw_1 can be rewritten:
String items = "Refresh | insert | Delete | modify"
Popmenu (this, items, this. x + this. pointerx (), this. y + this. pointery ())
Note: In the MDI application, the popmenu () function must be changed:
Popmenu (this, items, w_frame.pointerx (), w_frame.pointery () where w_frame is the name of the MDI main window.
---- In the popmenu function, the ue_beforepop user event of the right-click object is activated. If necessary, you can right-click
The object defines a ue_beforepop event. In this event, you can call the om_1.setitemdisable () function to shield a menu item.
---- Finally, do not forget to add the following statements to the application close event to release the system memory in time.
---- If isvalid (om_1) then destroy om_1
---- The above Code is developed on the Windows98 and Powerbuilder6.5 platforms and applied in multiple large systems. The results are very good.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.