Author: BALLOONMAN2002 2004 Nian 6 Yue 26 Ri
This article intends to combine the PowerBuilder language, briefly describe how to implement dynamically add menu effects. In the actual development process, many times due to the need for permission control, dynamically add menu items according to the user's different permissions, how to avoid use of cumbersome API functions (such as CreateMenu, InsertMenu, InsertMenuItem, Appendmenu, etc.), using PowerBuilder language itself What is this? This article will be simply discussed on this issue.
First, basic ideas
In the Menu Object section of the PB help, there is one of its Properties description:
Item [] Menu Specifier The Menu Objects Under a menu Object.
This seems simple sentence in fact, there is a three-layer meaning:
1. This sentence shows that the MENU object has an attribute as an array type. Its each element records all of its specific sub-item, which can use each element in this array to access the submenu of the underlying;
2, these subjects are still the MENU type, so it can be extended to the next layer according to the logic of the first point, and can be accessed by the second layer menu of the sub-item [] attribute. It is possible to fully traverse all submenu items of the entire menu system;
3, since this attribute is an array type, then follow the array type variables, we can perform function operations such as UpperBound (), LowerBound (), you can also add new projects, this It is very important, it is the core of our entire article, how to use the PB implementation dynamic add menu, it is to add this array element (reader must have a clear understanding of this), while there are some supporting specific implementation skills, details as follows.
Second, the specific implementation
(1) Basic work
Create a Window window and two menu menus, one menu is named m_main, used as the basic master menu of the window, the new menu will be added thereon, the menu can include some basic menus, such as files, add, delete , Save, exit; another menu is named m_new, which only has a main menu Item, there is no further submenu under this, so the menu is actually a menu item.
Declaring instance variables in the main window:
Menu im_new [] // declare a menu array for reference new menu
Integer II_COUNT / / Declaration Variable Record A total of new menu numbers
(2) Specific code
1. Add a first-level menu function:
//
//
// Uses: Add a first-level menu after the last item and the menu
// Time: 2003-6-1
//
//
Integer li_itemcount // Declaration Variable Record The total number of current first-level menus;
/ / Create a menu object and put it in the menu array IM_NEW [];
II_count
IM_NEW [II_COUNT] = CREATE M_NEW
// Get reference to the newly created menu object;
LI_Itemcount = Upperbound (m_main.Item [])
m_main.item [li_itemcount 1] = IM_NEW [II_COUNT] .Item [1]
// The following is the text property setting new generation menu, which can be taken from the database, but it is omitted here; m_main.item [li_itemcount 1] .text = "Add first level -" String (li_itemcount 1)
// The following is to set the TAG property of the new generation menu, this property can be taken from the database, but it is omitted here.
// The TAG can be used as a function number or the like, and the function is treated by the GF_MenuMessage () function;
//m_main.Item [LI_Itemcount 1] .tag = "xxxxxxxx"
// This is to display the newly generated menu object;
m_main.item [1] .hide ()
m_main.item [1] .show ()
2, add the implementation of the secondary menu function:
//
//
// Uses: Add a second-level menu under the last level menu
// Time: 2003-6-1
//
//
// Declare the variable record total number of secondary menus corresponding to the last first level menu;
Integer Li_PRECount, LI_Itemcount
/ / Create a menu object and put it in the menu array IM_NEW [];
II_count
IM_NEW [II_COUNT] = CREATE M_NEW
// Get reference to the newly created menu object;
LI_PRECOUNT = Upperbound (m_main.Item [])
LI_Itemcount = Upperbound (m_main.item [li_precount] .Item [])
m_main.item [li_precount] .Item [li_itemcount 1] = im_new [ii_count] .Item [1]
// The following is the text property of setting a new generation menu, which can be taken from the database, but it is omitted here.
m_main.item [li_precount] .Item [li_itemcount 1] .text = "Add Level 2 -" String (Li_PRECount) " String (li_itemcount 1)
// The following is to set the TAG property of the new generation menu, this property can be taken from the database, but it is omitted here.
// The TAG can be used as a function number or the like, and the function is treated by the GF_MenuMessage () function;
//m_main.Item [LI_PRECount].item[li_itemcount 1] .tag = "xxxxxxxx"
// This is to display the newly generated menu object;
m_main.item [1] .hide ()
m_main.item [1] .show ()
3, add the implementation of the three-level menu function:
//
//
// Uses: Add a three-level menu under the last second-level menu
// Time: 2003-6-1
//
//
// Declare the variable record total number of secondary menus corresponding to the last first level menu;
Integer li_fircount, li_precount, li_itemcount
/ / Create a menu object and put it in the menu array IM_NEW [];
II_count
IM_NEW [II_COUNT] = CREATE M_NEW
// Get reference to the newly created menu object;
LI_FIRCOUNT = UpperBound (m_main.Item []) Li_PRECount = UpperBound (m_main.item [li_fircount] .Item [])
IF li_precount = 0 THEN
MessageBox ("Tips", please click - Add Second Order Menu - Button Create a secondary menu, :) ")
Return
END IF
LI_Itemcount = Upperbound (m_main.item [li_fircount] .Item [li_precount] .Item [])
m_main.item [li_fircount] .Item [li_precount] .Item [li_itemcount 1] = iM_new [ii_count] .Item [1]
// The following is the text property of setting a new generation menu, which can be taken from the database, but it is omitted here.
m_main.item [li_fircount] .Item [li_precount] .Item [li_itemcount 1] .text = "Add Level 3 -" String (Li_Fircount) &
"-" String (li_precount) "-" String (li_itemcount 1)
// The following is to set the TAG property of the new generation menu, this property can be taken from the database, but it is omitted here.
// The TAG can be used as a function number or the like, and the function is treated by the GF_MenuMessage () function;
//m_main.Item [LI_PRECount].item[li_itemcount 1] .tag = "xxxxxxxx"
// This is to display the newly generated menu object;
m_main.item [1] .hide ()
m_main.item [1] .show ()
4, deal with the Clicked event of the dynamically generated menu:
The overall thinking is to use parameterized ideas, with a unified function, depending on the function parameters, depending on the function parameters.
(1) Handling the Clicked event of the M_New object:
//
// Uses: According to the function number corresponding to the newly generated menu, the TAG attribute removed from the database
// To call the GF_MENUMESSAGE () global function to achieve different system functions;
// Time: 2003-6-1
//
// This should be called to call the GF_MENUMESSAGE () global function to handle the function number to be processed by different functions.
/ / But there is slightly here, and the details can be found in the GF_MenuMessage () function;
// only demonstrate a messagebox () function here, as follows;
// integer li_ret
// li_ret = GF_MenuMessage (this.tag)
// The following code is to avoid clicking on the menu with the next level submenu, the dialog box is popped, resulting in the case where the next menu cannot be displayed;
IF Upperbound (this.Item []> 0 THEN
Return
END IF
MessageBox ("Dynamic Menu Demo", "Your current click menu is:" this.text)
(2) Write a GF_MENUMESSAGE function:
//
// Uses: According to the function number passed from the newly generated menu, the TAG attribute removed from the database is dynamically determined by the operation;
// Time: 2003-6-1
// Enter: string type as_funcode: The function number to be implemented in the corresponding menu;
// Output: Integer type 1: success
// -1: Failure
//
// Note: According to the function number passed from the newly generated menu, the TAG attribute from the database is from the database.
// To dynamically determine the operations you want; this operation needs to connect to the database, so you are just a simple idea, specific
String ls_funcode // Declare a variable used to record the function number of the passed;
Ls_funcode = as_funcode
Choose Case Ls_Funcode
Case "1"
// ..., such as 1 represents the entry management, then call the entry management function GF_RK (), etc.
Case "2"
// ..., such as 1 represents the outbound management, then call the outbound management function GF_CK (), etc.
// ......
Case Else
Return -1
End chaoose
Return 1
At this point, the dynamic add menu effect is complete, the renderings are shown in:
http://blog.9cbs.net/images/blog_9cbs_net/balloonman2002/17312/r_add_menu.jpg
For further information, please contact QQ: 27855043, MSN: weiyin2001@msn.com
If you have something wrong, you respect your guidance.