none
How to add item to a popup menu? RRS feed

  • Question

  • I am trying to make a popup menu but I am stuck in this code:

    CMenu* tmpMenu = new CMenu();

    tmpMenu->CreatePopupMenu();

    tmpMenu->InsertMenu(0, MF_BYPOSITION, 0, 0);

    So I just donnt know how to add an item to the newly made menu.

     

    Can someone please help?

    Saturday, March 4, 2006 3:45 PM

Answers

  • Sample code from MSDN CreateMenu:

    // The code fragment below shows how to create a new menu for the
    // application window using CreateMenu() and CreatePopupMenu().
    // Then, the created menu will replace the current menu of the
    // application. The old menu will be destroyed with DestroyMenu().
    // NOTE: The code fragment below is done in a CFrameWnd-derived class.

    // Create a new menu for the application window.
    VERIFY(m_NewMenu.CreateMenu());

    // Create a "File" popup menu and insert this popup menu to the
    // new menu of the application window. The "File" menu has only
    // one menu item, i.e. "Exit".
    VERIFY(m_FileMenu.CreatePopupMenu());
    m_FileMenu.AppendMenu(MF_STRING, ID_APP_EXIT, (LPCTSTR)"E&xit");
    m_NewMenu.AppendMenu(MF_POPUP, (UINT) m_FileMenu.m_hMenu, "&File");

    Saturday, March 4, 2006 4:11 PM
    Moderator

All replies

  • Sample code from MSDN CreateMenu:

    // The code fragment below shows how to create a new menu for the
    // application window using CreateMenu() and CreatePopupMenu().
    // Then, the created menu will replace the current menu of the
    // application. The old menu will be destroyed with DestroyMenu().
    // NOTE: The code fragment below is done in a CFrameWnd-derived class.

    // Create a new menu for the application window.
    VERIFY(m_NewMenu.CreateMenu());

    // Create a "File" popup menu and insert this popup menu to the
    // new menu of the application window. The "File" menu has only
    // one menu item, i.e. "Exit".
    VERIFY(m_FileMenu.CreatePopupMenu());
    m_FileMenu.AppendMenu(MF_STRING, ID_APP_EXIT, (LPCTSTR)"E&xit");
    m_NewMenu.AppendMenu(MF_POPUP, (UINT) m_FileMenu.m_hMenu, "&File");

    Saturday, March 4, 2006 4:11 PM
    Moderator
  • I tried to implement this but really could not do it. I cannt understand this code. Can you please post a working chunk of code?

     

    (this is my code that doesn't work)


     CMenu* tmpMenu = new CMenu();
     MENUITEMINFO tmpItem;
     CString tmpStr = "Insert New";

     tmpItem.fMask = MIIM_STRING | MIIM_ID;
     tmpItem.fType = MFT_STRING;
     tmpItem.fState = MFS_ENABLED;
     tmpItem.wID = 0;
     tmpItem.hSubMenu = NULL;
     tmpItem.hbmpChecked = NULL;
     tmpItem.hbmpUnchecked = NULL;
     tmpItem.dwTypeData = (LPSTR) &tmpStr;
     tmpItem.cch = strlen(tmpStr);
     tmpItem.cbSize = sizeof(tmpItem);

     tmpMenu->CreatePopupMenu();
     tmpMenu->InsertMenuItem(0, (LPMENUITEMINFO) &tmpItem, 0);


     

     

    Saturday, March 4, 2006 7:58 PM
  • I fixed your code that works (VS 2005 MFC Dialog, not using Unicode).


    void CPopMenuDlg::OnRButtonUp(UINT nFlags, CPoint point)
    {
        // TODO: Add your message handler code here and/or call default

        CMenu* tmpMenu = new CMenu();
        MENUITEMINFO tmpItem;
        CString tmpStr = _T("Insert New");

        tmpItem.fMask = MIIM_STRING | MIIM_ID;
        tmpItem.fType = MFT_STRING;
        tmpItem.fState = MFS_ENABLED;
        tmpItem.wID = 0;
        tmpItem.hSubMenu = NULL;
        tmpItem.hbmpChecked = NULL;
        tmpItem.hbmpUnchecked = NULL;
        tmpItem.dwTypeData = (LPTSTR)tmpStr.GetBuffer();
        tmpItem.cch = tmpStr.GetLength();   
        tmpItem.cch = 10;
        tmpItem.cbSize = sizeof(tmpItem);

        tmpMenu->CreatePopupMenu();
        tmpMenu->InsertMenuItem(0, (LPMENUITEMINFO) &tmpItem, 0);

        CPoint pt;
        GetCursorPos(&pt);
        RECT rect;

        tmpMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,
            pt.x, pt.y, this, &rect);

        CDialog::OnRButtonUp(nFlags, point);
    }

    Sunday, March 5, 2006 7:05 AM
  • Thank you fot your replay, but I donnt know if it is helpful as I keep getting this error message:

     

    error C2065: 'MIIM_STRING' : undeclared identifier

    Sunday, March 5, 2006 10:14 AM
  • Ok, this error message is no more an issue.

    Now the menu is popping up, but it's merely a little box containing only a separator.

    How can I add a item to the menu?

    Sunday, March 5, 2006 12:15 PM
  •  Milos Kompjuteras wrote:

    Ok, this error message is no more an issue.

    Now the menu is popping up, but it's merely a little box containing only a separator.

    How can I add a item to the menu?

    Can you show your entire menu modification code snippet please?

    Sunday, March 5, 2006 2:19 PM
    Moderator
  • Ok, this is the right click event for the CTabCtrl control:

     

    void CMFC02Dlg::OnNMRclickTab1(NMHDR *pNMHDR, LRESULT *pResult)
    {

        CMenu* tmpMenu = new CMenu();
        MENUITEMINFO tmpItem;
        CString tmpStr = _T("Insert New");

        tmpItem.fMask = MFT_STRING | MIIM_ID;
        tmpItem.fType = MFT_STRING;
        tmpItem.fState = MFS_ENABLED;
        tmpItem.wID = 0;
        tmpItem.hSubMenu = NULL;
        tmpItem.hbmpChecked = NULL;
        tmpItem.hbmpUnchecked = NULL;
        tmpItem.dwTypeData = (LPTSTR)&tmpStr;
        tmpItem.cch = tmpStr.GetLength();   
        tmpItem.cch = 10;
        tmpItem.cbSize = sizeof(tmpItem);

        int hTmpMenu = tmpMenu->CreatePopupMenu();
        tmpMenu->InsertMenuItem(1, (LPMENUITEMINFO) &tmpItem, false);

        CPoint pt;
        GetCursorPos(&pt);
        tmpMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, pt.x, pt.y, this, 0);
    }

    Sunday, March 5, 2006 2:38 PM
  • The results are different between VS6.0, VS6.0 SP6 and VS 2005. As I mentioned above, I'm using VS 2005 non Unicode.
    For VS6.0, MIIM_STRING and InsertMenuItem are not declared, so you got error message.
    For VS6.0 SP6, some fixes for these symbol and member function of CMenu. Added InsertMenuItem() function to CMenu. You should download the Service Pack 6 from MS site for VS6.0 if you'r using VS6.0.
    My code is for VS 2005 MFC Dialog, it works correctly as I had compiled and ran before posting here.

    The problem may be due to string passed. You should check if your project is using UNICODE or not. Pass ANSI character string is good for menu item.

    Further more,
    tmpItem.cch = tmpStr.GetLength();   
    tmpItem.cch = 10; <-- this code is dummy and need be removed


    Sunday, March 5, 2006 2:56 PM
  • Thank you all for your help! I finally included this line in my code:

    tmpMenu->InsertMenu (0, MF_BYPOSITION, hTmpMenu, tmpStr);

    so it all works fine now.

    This menu has only 1 item but I will now add more items and hopefully grant them some functionality :)

    void CMFC02Dlg::OnNMRclickTab1(NMHDR *pNMHDR, LRESULT *pResult)

    {

    CMenu* tmpMenu = new CMenu();

    MENUITEMINFO tmpItem;

    CString tmpStr = _T("Insert New");

    tmpItem.fMask = MFT_STRING | MIIM_ID;

    tmpItem.fType = MFT_STRING;

    tmpItem.fState = MFS_ENABLED;

    tmpItem.wID = 0;

    tmpItem.hSubMenu = NULL;

    tmpItem.hbmpChecked = NULL;

    tmpItem.hbmpUnchecked = NULL;

    tmpItem.dwTypeData = (LPTSTR)&tmpStr;

    tmpItem.cch = tmpStr.GetLength();

    tmpItem.cch = 10;

    tmpItem.cbSize = sizeof(tmpItem);

    int hTmpMenu = tmpMenu->CreatePopupMenu();

    tmpMenu->InsertMenuItem(1, (LPMENUITEMINFO) &tmpItem, false);

    tmpMenu->InsertMenu (0, MF_BYPOSITION, hTmpMenu, tmpStr);

    CPoint point;

    GetCursorPos(&point);

    tmpMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, this, 0);

    }

    Peace!

    Monday, March 6, 2006 8:39 AM
  • I try to create a menu popup, too. But I stuck at that code:

     

    CMenu  *m_MainMenu;
      CMenu  *SubMenu;
      CMenu  m_Popup;
     

    unsigned count;

     

     m_MainMenu = GetMenu();

     SubMenu = m_MainMenu -> GetSubMenu(0);

     count = SubMenu -> GetMenuItemCount();

     

    m_Popup.CreatePopupMenu();

     

    m_Popup.InsertMenu( 0, MF_BYPOSITION | MF_STRING, IDM_RESET, "&Reset" );
      m_Popup.InsertMenu( 1, MF_BYPOSITION | MF_STRING, IDM_SHOWTEXT, "&Show text" );

     

    SubMenu -> InsertMenu( count , MF_BYPOSITION | MF_SEPARATOR );

    SubMenu -> InsertMenu( count + 1, MF_BYPOSITION | MF_POPUP, (unsigned) m_Popup.m_hMenu, "&New Options"  );

    SubMenu -> InsertMenu( count + 2, MF_BYPOSITION | MF_STRING, IDM_RESET, "Reset");

     

    //-----------------------

     

    I select Add, Delete, then Add. It appear error warning like that:

     

    Debug Assertion Failed!

    File: winmenu.cpp

    Line: 98

     

    //-------------------------------------------------------------------------

    Second: istuck with this code to build floating menu

    //-------------------------------------------------------------------------

     

    afx_msg void CMainWin:SurprisenRButtonDown( UINT Flags, CPoint Loc )
    {
     CMenu *SubMenu;
     unsigned ucount;

     ClientToScreen(&Loc);

     m_FloatMenu.LoadMenu( "GraphMenu" );
     SubMenu = m_FloatMenu.GetSubMenu(0);

     ucount = SubMenu -> GetMenuItemCount();

     SubMenu -> InsertMenu( ucount, MF_BYPOSITION | MF_STRING, IDM_SHOWTEXT, "Addition" );
      
     m_Popup.CreatePopupMenu();
     m_Popup.InsertMenu( 0, MF_BYPOSITION | MF_STRING, IDM_RESET, "Thread" );
     m_Popup.InsertMenu( 1, MF_BYPOSITION | MF_STRING, IDM_SHOWTEXT, "Show" );
     SubMenu -> InsertMenu( ucount, MF_POPUP | MF_BYPOSITION, ( unsigned )m_Popup.m_hMenu, "New" );

     SubMenu -> TrackPopupMenu( 0, 500, 200, this );

     m_FloatMenu.DestroyMenu();
    }

    with the same error at the secondtimes I push the right button mouse.

     

     

    Wednesday, July 25, 2007 6:57 AM