Symbian menu 2-change menu at runtime to implement right-click menu Function
The symbian menu is mainly composed of MENU_BAR-> MENU_TITLE-> MENU_PANE-> MENU_ITEM
The titles attribute of MENU_BAR specifies the set of MENU_TITLE
Menu_pane of MENU_TITLE specifies the MENU_TITLE which MENU_PANE provides the menu item
The items of MENU_PANE specifies the menu set, which is composed of MENU_ITEM
In MENU_ITEM, command is the id of the menu command, txt is the text displayed in the menu, and cascade is the MENU_PANE of the associated sub-menu.
Add the menu to the system menu in
RESOURCE EIK_APP_INFO { Hotkeys = r_contentm_hotkeys; Menubar = r_contentm_menubar; CBA = R_AVKON_SOFTKEYS_OPTIONS_BACK; } |
Add to menubar
Now the question is how to change the menu at runtime so that the menu can be displayed by itself.
CEikMenuBar * bar = iEikonEnv-> appuifacloud ()-> MenuBar (); |
After obtaining this bar, you can manually load a menu using the following method, as shown in the following code:
Bar-> SetMenuTitleResourceId (R_CONTENT_MENU ); If (bar) { Bar-> TryDisplayMenuBarL (); } Bar-> SetMenuTitleResourceId (R_CONTENTM_MENUBAR ); |
SetMenuTitleResourceId is used to load a menu by loading a resource number. After loading, TryDisplayMenuBar () is used to display the menu. After the menu is used, SetMenuTitleResourceId () is used to load the default menu, in this way, you can dynamically change the menu or implement a shortcut menu effect.
Through the menu exercises, I also learned how to use Listbox, but it is not clear that all of the following code (the code is only for self-written and not automatically generated by the system)
RSS File Code
RESOURCE MENU_BAR r_content_menu { Titles = { MENU_TITLE {menu_pane = r_content_item; txt = "";} }; } RESOURCE MENU_PANE r_content_item { Items = { MENU_ITEM {command = EContentMCmdDeleteGameLevel; txt = qtn_app_deleteGameLevel ;}, MENU_ITEM {command = EContentMCmdPlayGameLevel; txt = qtn_app_playGameLevel ;} }; } Resource listbox r_listbox { Array_id = r_levelItem; Flags = EAknListBoxSelectionList;
} Resource array r_levelItem { Items = { LBUF {txt = qtn_app_level1 ;}, LBUF {txt = qtn_app_level2 ;}, LBUF {txt = qtn_app_level3 ;}, LBUF {txt = qtn_app_level4 ;}, LBUF {txt = qtn_app_level5 ;}, LBUF {txt = qtn_app_level6 ;}, LBUF {txt = qtn_app_level7 ;}, LBUF {txt = qtn_app_level8 ;}, LBUF {txt = qtn_app_level9 ;} }; } |
HRH File Code
Enum TGameCmdIds { EContentMCmdDeleteGameLevel = 2, EContentMCmdPlayGameLevel }; |
LOC File Code
# Define qtn_app_deleteGameLevel "delete level" # Define qtn_app_playGamelevel "play level" # Define qtn_app_level1 "\ tlevel 1" # Define qtn_app_level2 "\ tlevel 2" # Define qtn_app_level3 "\ tlevel 3" # Define qtn_app_level4 "\ tlevel 4" # Define qtn_app_level5 "\ tlevel 5" # Define qtn_app_level6 "\ tlevel 6" # Define qtn_app_level7 "\ tlevel 7" # Define qtn_app_level8 "\ tlevel 8" # Define qtn_app_level9 "\ tlevel 9" |
After all the files are configured, add the code
Container File
# Include "ContentMContainer. h" # Include "ContentM. RSG" # Include <EIKENV. H> # Include <barsread. h> # Include <aknlists. h> # Include <eikmenub. h> # Include <uikon. hrh> // =========================== Member functions ============================ ==== //--------------------------------------------------------- // CContentMContainer: ConstructL (const TRect & aRect) // EPOC two phased constructor //--------------------------------------------------------- // Void CContentMContainer: ConstructL (const TRect & aRect) { Createmediawl (); ISaveLevel = new (ELeave) CAknSingleStyleListBox; ISaveLevel-> setcontainer1_wl (* this ); TResourceReader reader; CEikonEnv: Static ()-> CreateResourceReaderLC (reader, R_LISTBOX ); ISaveLevel-> ConstructFromResourceL (reader ); CleanupStack: PopAndDestroy (); SetRect (aRect ); ActivateL (); } // Destructor CContentMContainer ::~ CContentMContainer () { Delete iSaveLevel; } //--------------------------------------------------------- // CContentMContainer: SizeChanged () // Called by framework when the view size is changed //--------------------------------------------------------- // Void CContentMContainer: SizeChanged () { // TODO: Add here control resize code etc. ISaveLevel-> SetExtent (TPoint (0, 0), iSaveLevel-> MinimumSize ()); } //--------------------------------------------------------- // CContentMContainer: CountComponentControls () const //--------------------------------------------------------- // TInt CContentMContainer: CountComponentControls () const { Return 1; // return nbr of controls inside this container } //--------------------------------------------------------- // CContentMContainer: ComponentControl (TInt aIndex) const //--------------------------------------------------------- // CCoeControl * CContentMContainer: ComponentControl (TInt aIndex) const { Switch (aIndex) { Case 0: Return iSaveLevel;
Default: Return NULL; } } //--------------------------------------------------------- // CContentMContainer: Draw (const TRect & aRect) const //--------------------------------------------------------- // Void CContentMContainer: Draw (const TRect & aRect) const { CWindowGc & gc = SystemGc (); // Gc. Clear (aRect ); // TODO: Add your drawing code here // Example code... Gc. SetPenStyle (CGraphicsContext: ENullPen ); Gc. SetBrushColor (KRgbGray ); Gc. SetBrushStyle (CGraphicsContext: ESolidBrush ); // Gc. DrawRect (aRect ); } //--------------------------------------------------------- // CContentMContainer: HandleControlEventL ( // CCoeControl * aControl, TCoeEvent aEventType) //--------------------------------------------------------- // Void CContentMContainer: HandleControlEventL ( CCoeControl */* aControl */, TCoeEvent/* aEventType */) { // TODO: Add your control event handler code here } Void CContentMContainer: DeleteGameLevel () { If (iSaveLevel) { CTextListBoxModel * model = iSaveLevel-> Model (); If (model-> NumberOfItems ()> 0) { MDesCArray * itemList = model-> ItemTextArray (); CDesCArray * itemArray = STATIC_CAST (CDesCArray *, itemList ); TInt currentItem = iSaveLevel-> CurrentItemIndex (); ItemArray-> Delete (currentItem ); AknListBoxUtils: HandleItemRemovalAndPositionHighlightL (iSaveLevel, currentItem, ETrue ); ISaveLevel-> DrawNow (); } } } Void CContentMContainer: PlayGameLevel () { } TKeyResponse CContentMContainer: OfferKeyEventL (const TKeyEvent & aKeyEvent, TEventCode aType) { // TBool isClickOk = (aKeyEvent. iCode = EKeyDevice3) & (aType = EEventKey )); TBool isClickOk = (aKeyEvent. iCode = EKeyOK) & (aType = EEventKey )); TBool ListBoxNotEmpty = (iSaveLevel) & (iSaveLevel-> Model ()-> NumberOfItems ()); If (isClickOk & ListBoxNotEmpty) { CEikMenuBar * bar = iEikonEnv-> appuifacloud ()-> MenuBar (); Bar-> SetMenuTitleResourceId (R_CONTENT_MENU ); If (bar) { Bar-> TryDisplayMenuBarL (); } Bar-> SetMenuTitleResourceId (R_CONTENTM_MENUBAR ); Return EKeyWasConsumed; } Else Return iSaveLevel-> OfferKeyEventL (aKeyEvent, aType ); } // End of File |
The red part is the main code
The header file is a simple one.
CEikTextListBox * iSaveLevel; |
You can use AppUI to process menus.
Processing in HandleCommand of AppUI
Void CContentMAppUi: HandleCommandL (TInt aCommand) { Switch (aCommand) { Case EAknSoftkeyBack: Case EEikCmdExit: { Exit (); Break; } Case EContentMCmdAppTest: { IEikonEnv-> InfoMsg (_ L ("test ")); Break; } // TODO: Add Your command handling code here Case EContentMCmdDeleteGameLevel: { IAppContainer-> DeleteGameLevel (); Break; } Case EContentMCmdPlayGameLevel: { IAppContainer-> PlayGameLevel (); Break; } Default: Break;
} |
The red part is used to process the menu. I still don't know how to delete the selected ListBox item. I should understand it when I learn listbox.
Anping 2009 @ original
Qi_jianzhou@126.com