CMenu::GetSubMenu()

rtn
rtn used Ask the Experts™
on
I am trying to get a popup menu to display when the right mouse button is clicked. However CMenu::GetSubMenu() seems to be returning a NULL pointer, and I don't know why!
Here is my code:-

void CMenuView::OnContextMenu(CWnd* pWnd, CPoint point)
{
      // TODO: Add your message handler code here
      CMenu   zooMenu;
      CMenu  *pPopup ;
      int Test;
    // Store popup point, and convert to client coordinates
    // for the drawing functions

    m_ptMsg = point;
    ScreenToClient( &m_ptMsg );

    Test = zooMenu.LoadMenu( IDR_MENUPOP );
      if (Test == 0 )
      {
            AfxMessageBox("unsuccessful");
      }

    pPopup = zooMenu.GetSubMenu( 0 );
      if (*pPopup == NULL)
      {
            AfxMessageBox("!");
      }
      else
      {
            AfxMessageBox("$");
      
    pPopup->TrackPopupMenu( TPM_LEFTALIGN|TPM_RIGHTBUTTON,
                            point.x,
                            point.y,
                            this) ;
      }
}

Thanks for your help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
rtn

Author

Commented:
Edited text of question.

Commented:
   Your code seems to be right.  Try putting the same code in WM_RBUTTONDOWN message handler.

    This is the code I used and it seemed to work:-
    CMenu menu;
    VERIFY(menu.LoadMenu(IDR_POPUP_MENU));
    CMenu* pPopup = menu.GetSubMenu(0);
    ASSERT(pPopup != NULL);

    CPoint point;
    GetCursorPos(&point);
    pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON,
        point.x, point.y, this);
One of the reason that you r getting NULL pointer can be that IDR_MENUPOP menu is not a popup menu. Check its style inthe resource editor. Make sure that Popup menu option is checked.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Commented:
The only reason for CMenu::GetSubMenu(0) to return NULL is that there's not popup menu at position 0 of your IDR_POPUP_MENU.

Make sure that there is one at position 0 inside your IDR_POPUP_MENU, and GetSubMenu(0) will function correctly.

If IDR_POPUP_MENU already IS a popup menu itself, use CMenu::TrackPopupMenu() directly instead.

Hope this helps,
Bob
rtn

Author

Commented:
I have removed the GetSubMenu() function in order to use TrackPopupMenu() directly.

pPopup = zooMenu.GetSubMenu( 0 );     is now

pPopup = &zooMenu;

Now it works apart from only a 3mm vertical strip is displayed. From this strip the menu items can be selected correctly, but the menu can not be seen properly.
Thanks for your help.
rtn

Author

Commented:
I have removed the GetSubMenu() function in order to use TrackPopupMenu() directly.

pPopup = zooMenu.GetSubMenu( 0 );     is now

pPopup = &zooMenu;

Now it works apart from only a 3mm vertical strip is displayed. From this strip the menu items can be selected correctly, but the menu can not be seen properly.
Thanks for your help.
rtn

Author

Commented:
I have removed the GetSubMenu() function in order to use TrackPopupMenu() directly.

pPopup = zooMenu.GetSubMenu( 0 );     is now

pPopup = &zooMenu;

Now it works apart from only a 3mm vertical strip is displayed. From this strip the menu items can be selected correctly, but the menu can not be seen properly.
Thanks for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial