Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

popup menu

webcrawlerwebcrawler Member Posts: 12
The directions in sam's "teach yourself visual c++ in 24 hours" tell the reader to add a menu which is simple enough, then toadd a single space as a dummy caption for the first top-level item on the menu bar. then to open the properties dialog box for the menu resource by right clicking the edge of the menu resource and change the resource ID to ID_POPUP. Then to add three menu items under the dummy label:
ID_LIONS &Lions
ID_TIGERS &Tigers
ID_BEARS &Bears

Then you add two member variables for the CMenuView class which i've done
protected:
CPoint m_ptMsg;
CString m_szMsg;

The constructor is:
CMenuView::CMenuView()
{
m_ptMsg = CPoint(0,0);
}

the CMenuView::OnDraw member function is:
void CMenuView::OnDraw(CDC* pDC)
{
pDC->TextOut(m_ptMsg.x, m_ptMsg.y, m_szMsg);
}

Then I added message handling functions for everything:
CMenuView WM_CONTEXTMENU OnContextMenu()
ID_LIONS COMMAND OnLions()
ID_TIGERS COMMAND OnTigers()
ID_BEARS COMMAND OnBears()

Then the code for the OnContextMenu function:
void CMenuView::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu zooMenu;
//store popup point, and convert to client coordinates
//for the drawing functions
m_ptMsg = point;
ScreenToClient(&m_ptMsg);
zooMenu.LoadMenu(ID_POPUP);
CMenu* pPopup = zooMenu.GetSubMenu(0);
pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,
point.x,
point.y,
this);
}

Then the message handling functions for the floating menu items
void CMenuView::OnLions()
{
m_szMsg = "Lions are out";
InvalidateRect(NULL);
}

void CMenuView::OnTigers()
{
m_szMsg = "Tigers are afoot";
InvalidateRect(NULL);
}

void CMenuView::OnBears()
{
m_szMsg = "Bears are hungry";
InvalidateRect(NULL);
}

It builds fine but when i right click over the program window i get an assertion error. I'm pretty sure i've done everything correctly except where the dummy label is added and adding the ID to ID_POPUP is not too clear in the book.

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    I'm guessing it's in the LoadMenu(ID_DIALOG) code.
    It's really been too long since I've used MFC, but I'd run through the process of adding the menu again, checking to make sure you did everything right. Also, you could look back at previous examples how you added ID's then.

    I'd actually also suggest to set a Debugging Breakpoint on the OnContextMenu event and running through the code step by step with the debugger. But... I'm not sure if you're familair with that yet.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • webcrawlerwebcrawler Member Posts: 12
    : I'm guessing it's in the LoadMenu(ID_DIALOG) code.
    : It's really been too long since I've used MFC, but I'd run through
    : the process of adding the menu again, checking to make sure you did
    : everything right. Also, you could look back at previous examples how
    : you added ID's then.
    :
    : I'd actually also suggest to set a Debugging Breakpoint on the
    : OnContextMenu event and running through the code step by step with
    : the debugger. But... I'm not sure if you're familair with that yet.
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry
    On the resource view the new menu is called IDR MENU1. Is this id you use on LoadMenu or is it the ID under the general tab when you right click on the actual menu and select properties?
  • webcrawlerwebcrawler Member Posts: 12
    : : I'm guessing it's in the LoadMenu(ID_DIALOG) code.
    : : It's really been too long since I've used MFC, but I'd run through
    : : the process of adding the menu again, checking to make sure you did
    : : everything right. Also, you could look back at previous examples how
    : : you added ID's then.
    : :
    : : I'd actually also suggest to set a Debugging Breakpoint on the
    : : OnContextMenu event and running through the code step by step with
    : : the debugger. But... I'm not sure if you're familair with that yet.
    : :
    : : Best Regards,
    : : Richard
    : :
    : : The way I see it... Well, it's all pretty blurry
    : On the resource view the new menu is called IDR MENU1. Is this id
    : you use on LoadMenu or is it the ID under the general tab when you
    : right click on the actual menu and select properties?
    :
    I figured this out. thanks for the help
Sign In or Register to comment.