我想在菜单上帮助的傍边加一个叫New的菜单,如下
CMenu* pMenu=GetMenu();
pMenu->AppendMenu  (MF_POPUP, ID_FILE, "&NEW" );
SetMenu (pMenu);  这样可以
然后我想在New下加一个菜单New1,如下
pMenu = pMenu->GetSubMenu  (4);
pMenu->AppendMenu  (MF_STRING, ID_FILE1, "file");
就不行了,
菜单上原来有4个,New是第五个,把GetSubMenu  (4);改成GetSubMenu  (3);GetSubMenu  (2);都可以,请教!

解决方案 »

  1.   

    我刚刚也试了,好象也不行。
    查了一下MSDN里的:
    CMenu::GetSubMenu 
    Retrieves the CMenu object of a pop-up menu.CMenu* GetSubMenu(
       int nPos 
    ) const;
    Parameters
    nPos 
    Specifies the position of the pop-up menu contained in the menu. Position values start at 0 for the first menu item. The pop-up menu's identifier cannot be used in this function. 
    Return Value
    A pointer to a CMenu object whose m_hMenu member contains a handle to the pop-up menu if a pop-up menu exists at the given position; otherwise NULL. If a CMenu object does not exist, then a temporary one is created. The CMenu pointer returned should not be stored.
    其中有一句The pop-up menu's identifier cannot be used in this function.
    可能是因为这个吧。
      

  2.   

    CMenu* pMainMenu = GetMenu(); CMenu* pNewMenu = new CMenu; pNewMenu->CreatePopupMenu();
    pNewMenu->AppendMenu(MF_STRING, ID_FILE_SAVE_AS, "Save As"); pMainMenu->AppendMenu(MF_POPUP, (UINT) pNewMenu->m_hMenu, "&NewMenu"); SetMenu (pMainMenu);  
      

  3.   

    我今天终于弄明白了:
    CMenu m_FileMenu;
    VERIFY(m_FileMenu.CreatePopupMenu());
    CMenu *pMenu = GetMenu();
    pMenu->AppendMenu(MF_POPUP, (UINT) m_FileMenu.m_hMenu, "&NEW" );
    CMenu *pSubMenu = pMenu->GetSubMenu(4);//新添加的菜单索引为4
    pSubMenu->AppendMenu(MF_STRING, ID_FILE1, "file");
    SetMenu(NULL);
    SetMenu(pMenu);
    m_hMenuDefault = pMenu->m_hMenu;
    pMenu->Detach();
    m_FileMenu.Detach();