Adding a menu item dynamically when ID of DropDown is not known

Article ID: 870
Last updated: 05 Feb, 2008
Article ID: 870
Last updated: 05 Feb, 2008
Revision: 1
Views: 2688
Posted: 02 Sep, 1997
by Dean J.
Updated: 05 Feb, 2008
by Dean J.
Problem


Adding menu items dynamically to an already existing menu created by zFactory.


Cause


This is because the .rc file attaches IDs to sub-menu items, not the DropDown itself. So you can not simply say get(ID) to get a handle to the submenu item.


Action


//============================================================================
// Adding a new menu item dynamically.
// This adds The Other item between Edit|Cut and Edit|Paste as a test.
// The new item is added when menu item Change|AddItem is selected.
// The new item even has a working event handler associated with it.
//============================================================================
// In HPP file

class WMain : public zMDIAppFrame {
 
        ...
 // zpb_begin WMainClass
        int cmdEditTheOther(zCommandEvt* ev);   //<======== new event handler
 // zpb_end
};

//============================================================================
// In CPP file
// (Starting with factory generated code for menu item Change|Additem).

int WMain::cmdChangeAddItem(zCommandEvt* ev) {
   // zpb_begin WMainChangeAddItem
                                         //=== The next 3 lines are Win32 API.
   HMENU hMenu = GetMenu( *this );
   HMENU hSubMenu = GetSubMenu( hMenu, 1 );
   InsertMenu( hSubMenu, 2, MF_BYPOSITION, IDM_KENTHEOTHER,
      (char*)zString(zResId(IDM_KENTHEOTHER)) );
   menu()->setCommand(this, (CommandProc)&WMain::cmdEditTheOther,
      IDM_KENTHEOTHER );

   // zpb_end
   return 0;
}
 

// Event handler for new menu item (programmer written code in protect block)

// zpb_begin WMainMemberFunctions
int WMain::cmdEditTheOther(zCommandEvt* ev) {
 delete new zMessage( this, The Other!, WMain, MB_OK );
   return 0;
}
// zpb_end
 

//============================================================================
// In H file

// zpb_begin AdditionalIDs
#define IDM_KENTHEOTHER         10417  //<=========== Any unique number
// zpb_end

//============================================================================
// In RC file

// zpb_begin RCFile
STRINGTABLE {
        IDM_KENTHEOTHER, The Other
}
// zpb_end
 

This article was:   Helpful | Not helpful
Report an issue
Article ID: 870
Last updated: 05 Feb, 2008
Revision: 1
Views: 2688
Posted: 02 Sep, 1997 by Dean J.
Updated: 05 Feb, 2008 by Dean J.

Others in this category