How to insert a menu item in main menu bar?

Ivan.Zhang
2014-04-30
2014-05-01
  • Ivan.Zhang

    Ivan.Zhang - 2014-04-30

    Hi David,

    I tried to insert a new menu item to main menu bar, but the item didn't inserted.

    I made some code change based on the "Frame" sample project.

    Could you give me some suggestion? thank you.

    void CMainFrame::OnInitialUpdate()
    {
    // The frame is now created.
    // Place any additional startup code here.

    GetFrameMenu()->InsertMenu(4, MF_BYPOSITION | MF_POPUP, (UINT_PTR)::CreatePopupMenu(), L"Text");
    GetFrameMenu()->AppendMenu(MF_BYPOSITION | MF_POPUP, (UINT_PTR)::CreatePopupMenu(), L"Text");
    
    DrawMenuBar();
    
    TRACE("Frame created\n");
    

    }

     
  • David

    David - 2014-05-01

    Hi

    Hi,

    The following code demonstrates how add a menu item to the frame's menu. Here we create the menu dynamically, but it would have been simpler to define the Test menu item in the resource script and load it from there.

    void CMainFrame::OnInitialUpdate()
    {
    // The frame is now created.
    // Place any additional startup code here.
    
    TRACE("Frame created\n");
    
    // Create the Test menu and its submenu
    static CMenu Test;
    Test.CreateMenu();
    static CMenu TestSub;
    TestSub.CreatePopupMenu();
    
    // Set the info of the first item in the submenu
    MENUITEMINFO mii = {0};
    mii.cbSize = GetSizeofMenuItemInfo();
    mii.fType = MFT_STRING;
    mii.fMask = MIIM_TYPE | MIIM_ID;
    mii.dwTypeData = _T("First Item");
    mii.cch = lstrlen(mii.dwTypeData);
    mii.wID = IDM_EDIT_COPY;
    TestSub.InsertMenuItem(0, &mii, TRUE);
    
    // Set the info of the second item in the submenu
    mii.dwTypeData = _T("Second Item");
    mii.cch = lstrlen(mii.dwTypeData);
    mii.wID = IDM_EDIT_PASTE;
    TestSub.InsertMenuItem(1, &mii, TRUE);
    
    // Set the Test menu item's info and specify its submenu 
    mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
    mii.dwTypeData = _T("Test");
    mii.cch = lstrlen(mii.dwTypeData);
    mii.hSubMenu = TestSub;
    
    // Insert the Test menu item to position 0 in the frame's menu
    GetFrameMenu()->InsertMenuItem(0, &mii, TRUE);
    
    // Update the MenuBar's menu with the modified FrameMenu
    GetMenuBar()->SetMenu(*GetFrameMenu());
      }
    

    The CMenu variables are declared as static here, otherwise the menu they contain would be destroyed when InitInstance ends and the CMenu objects are deconstructed. Static variables are fine for demonstration purposes like this, but in production code we would find some other way of keeping them in scope, such as making them class member variables.

    Best regards,
    David

     
  • Ivan.Zhang

    Ivan.Zhang - 2014-05-01

    David, thank you for your quick response.
    It works after I added the following code.

    // Update the MenuBar's menu with the modified FrameMenu
    GetMenuBar()->SetMenu(*GetFrameMenu());

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks