locked
how to dynamically add popup menu as menu item under FILE RRS feed

  • Question

  • In my menu, the default menu items under FILE are:

    File                Edit  View
       -> New
       -> Open
        ...etc...

    I want to programmatically/dynamically add a pop menu as a menu item under File:

    File
       -> New
       -> MyPopupMenu
       -> Open

    where the menu items under MyPopupMenu are:

      -> MyPopupMenu 
           -> Item 0
           -> Item 1
           -> Item 2

    Regardless of what I do, MyPopupMenu is always incorrectly inserted to the right of FILE.   That is, my menu appears as follows:

    File MyPopupMenu Edit View

    The code below illustrates how I am attempting to add MyPopupMenu.  How do I modify the code so that MyPopUpMenu appears as a menu item under FILE instead of to the right of FILE in the menu?

    Thanks in advance

    Ian

     

     

     

    // sample code called at the end of CMainFrame::LoadFrame()

     CMenu* menu = GetMenu();
     ASSERT( NULL != menu  &&  ::IsMenu( *menu ) );

     CMenu popupMenu;
     VERIFY( popupMenu.CreatePopupMenu() );
     UINT startID = 38040;
     VERIFY( popupMenu.AppendMenu( MF_STRING, startID + 0, _T("Item #0") ) );
     VERIFY( popupMenu.AppendMenu( MF_STRING, startID + 1, _T("Item #1") ) );
     VERIFY( popupMenu.AppendMenu( MF_STRING, startID + 2, _T("Item #2") ) );

     MENUITEMINFO info = { 0 };
     info.cbSize = sizeof( MENUITEMINFO );
     info.fMask = MIIM_SUBMENU | MIIM_STRING;
     info.hSubMenu = popupMenu.Detach();
     info.dwTypeData = _T("MyPopupSubMenu") ;
     VERIFY( menu->InsertMenuItem( 1, &info, TRUE ) );

     


    • Edited by IB00 Wednesday, July 2, 2014 5:28 AM
    Wednesday, July 2, 2014 5:24 AM

Answers

  • A quick and dirty way could be tih modificated code piece:

     CMenu* menu = GetMenu();
     ASSERT( NULL != menu  &&  ::IsMenu( *menu ) );
     CMenu popupMenu;
     VERIFY( popupMenu.CreatePopupMenu() );
     UINT startID = 38040;
     VERIFY( popupMenu.AppendMenu( MF_STRING, startID + 0, _T("Item #0") ) );
     VERIFY( popupMenu.AppendMenu( MF_STRING, startID + 1, _T("Item #1") ) );
     VERIFY( popupMenu.AppendMenu( MF_STRING, startID + 2, _T("Item #2") ) );
     MENUITEMINFO info = { 0 };
     info.cbSize = sizeof( MENUITEMINFO );
     info.fMask = MIIM_SUBMENU | MIIM_STRING;
     info.hSubMenu = popupMenu.Detach();
     info.dwTypeData = _T("MyPopupSubMenu") ;
     //VERIFY( menu->InsertMenuItem( 1, &info, TRUE ) );
     CMenu* fileMenu = menu->GetSubMenu(0);
     fileMenu->InsertMenuItem(2, &info, TRUE);

    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.

    • Marked as answer by IB00 Wednesday, July 2, 2014 4:10 PM
    Wednesday, July 2, 2014 7:09 AM
  • Correctly inserting the popup menu requires 3 steps:

    1) create and initialize the popup menu

    2) insert the popup menu using CMenu::InsertMenuItem()

    3) setting the name of the popup menu using CMenu::ModifyMenu.

    Ian

    • Marked as answer by IB00 Wednesday, July 2, 2014 4:10 PM
    Wednesday, July 2, 2014 4:10 PM

All replies

  • A quick and dirty way could be tih modificated code piece:

     CMenu* menu = GetMenu();
     ASSERT( NULL != menu  &&  ::IsMenu( *menu ) );
     CMenu popupMenu;
     VERIFY( popupMenu.CreatePopupMenu() );
     UINT startID = 38040;
     VERIFY( popupMenu.AppendMenu( MF_STRING, startID + 0, _T("Item #0") ) );
     VERIFY( popupMenu.AppendMenu( MF_STRING, startID + 1, _T("Item #1") ) );
     VERIFY( popupMenu.AppendMenu( MF_STRING, startID + 2, _T("Item #2") ) );
     MENUITEMINFO info = { 0 };
     info.cbSize = sizeof( MENUITEMINFO );
     info.fMask = MIIM_SUBMENU | MIIM_STRING;
     info.hSubMenu = popupMenu.Detach();
     info.dwTypeData = _T("MyPopupSubMenu") ;
     //VERIFY( menu->InsertMenuItem( 1, &info, TRUE ) );
     CMenu* fileMenu = menu->GetSubMenu(0);
     fileMenu->InsertMenuItem(2, &info, TRUE);

    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.

    • Marked as answer by IB00 Wednesday, July 2, 2014 4:10 PM
    Wednesday, July 2, 2014 7:09 AM
  • Thanks Bordon, with one minor hiccup, your suggestion adds the popup menu as an item below FILE.  

    The current hiccup (i.e. problem) is the menu item label is now blank (i.e. the text 'MyPopupSubMenu' does not appear as the menu item label).   I spent all day yesterday 'fighting' with CMenu and can no longer see the forest for the trees.   Would you have any idea of why the menu item label is now blank instead of showing 'MyPopupSubMenu'?   I updated the test code to use 'InsertMenuItem' as illustrated below.

    Thanks,

    Ian


     CMenu* menu = wnd->GetMenu();
     ASSERT( NULL != menu  &&  ::IsMenu( *menu ) );

     CMenu popupMenu;
     VERIFY( popupMenu.CreatePopupMenu() );
     UINT startID = 38032;
     for( int x = 0; x < 3;  ++x ) {
      TCHAR label[50];
      _stprintf( label, _T("Item %i"), x );

      MENUITEMINFO info = { 0 };
      info.cbSize = sizeof( MENUITEMINFO );
      info.fMask = MIIM_ID | MIIM_STATE | MIIM_TYPE;
      info.fType = MFT_STRING;
      info.dwTypeData = label;
      info.fState = MFS_ENABLED;
      info.wID = startID + x;
      VERIFY( popupMenu.InsertMenuItem( -1, &info, TRUE ) );
     }

     MENUITEMINFO info = { 0 };
     info.cbSize = sizeof( MENUITEMINFO );
     info.fMask = MIIM_STATE | MIIM_SUBMENU | MIIM_TYPE;
     info.fType = MFT_STRING;
     info.dwTypeData = _T("MyPopupSubMenu");
     info.fState = MFS_ENABLED;
     info.hSubMenu = popupMenu.Detach();
     CMenu* fileMenu = menu->GetSubMenu( 0 );
     ASSERT( NULL != fileMenu  &&  ::IsMenu( *fileMenu ) );
     VERIFY( fileMenu->InsertMenuItem( 1, &info, TRUE ) );

     wnd->DrawMenuBar( );

    • Edited by IB00 Wednesday, July 2, 2014 3:26 PM
    Wednesday, July 2, 2014 3:25 PM
  • Correctly inserting the popup menu requires 3 steps:

    1) create and initialize the popup menu

    2) insert the popup menu using CMenu::InsertMenuItem()

    3) setting the name of the popup menu using CMenu::ModifyMenu.

    Ian

    • Marked as answer by IB00 Wednesday, July 2, 2014 4:10 PM
    Wednesday, July 2, 2014 4:10 PM