例如我的程序有一主菜单,里边已有几个静态下拉菜单子项,如A、B、C现在我动态增加一下拉子菜单项D,加载已成功,已能正常显示,现在问题来了,我想在D下面继续增加子菜单
但不知为何,获取D的句柄总是NULL,但获取A、B、C的句柄都能成功 CMenu *pTopMenu = m_pMainWnd->GetMenu();
pTopMenu->AppendMenu(MF_STRING,ID_TEST,"子菜单D"); m_pMainWnd->DrawMenuBar();

if(m_pMainWnd->GetMenu()->GetSubMenu(0) == NULL) AfxMessageBox("A:NULL", MB_ICONASTERISK);
if(m_pMainWnd->GetMenu()->GetSubMenu(1) == NULL) AfxMessageBox("B:NULL", MB_ICONASTERISK);
if(m_pMainWnd->GetMenu()->GetSubMenu(2) == NULL) AfxMessageBox("C:NULL", MB_ICONASTERISK);
if(m_pMainWnd->GetMenu()->GetSubMenu(3) == NULL) AfxMessageBox("D:NULL", MB_ICONASTERISK);//该条件总是触发,也就是说菜单D的句柄总取不到,为何?在线等

解决方案 »

  1.   

    GetSubMenu
    The GetSubMenu function retrieves a handle to the drop-down menu or submenu activated by the specified menu item. HMENU GetSubMenu(
      HMENU hMenu,  // handle to menu
      int nPos      // menu item position
    );
    Parameters
    hMenu 
    [in] Handle to the menu. 
    nPos 
    [in] Specifies the zero-based relative position in the specified menu of an item that activates a drop-down menu or submenu. 
    Return Values
    If the function succeeds, the return value is a handle to the drop-down menu or submenu activated by the menu item. If the menu item does not activate a drop-down menu or submenu, the return value is NULL. 
      

  2.   


    CMenu pMenu;
    pMenu.CreatePopupMenu();

    GetMenu()->AppendMenu(MF_POPUP, (UINT)pMenu.m_hMenu, _T("AAAA") ); pMenu.AppendMenu(MF_STRING, 110, _T("AAA")); pMenu.AppendMenu(MF_STRING, 111, _T("BBB")); pMenu.Detach();
      

  3.   

    添加完成以后DrawMenuBar();刷新一下即可