Link to home
Start Free TrialLog in
Avatar of rtn
rtn

asked on

CMenu::GetSubMenu()

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.
Avatar of rtn
rtn

ASKER

Edited text of question.
   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.
ASKER CERTIFIED SOLUTION
Avatar of BSoeters
BSoeters

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rtn

ASKER

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.
Avatar of rtn

ASKER

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.
Avatar of rtn

ASKER

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.