Context Menu & Dialog Box :: MFC
Hi.
I would like to implement a context menu inside a dialog box. I created a "clear" menu (no caption) using Resource Editor. Note that I create an entirely new menu item like IDR_MAINFRAME (mine is IRD_CONTEXTMENU1. Inside the dialog box, I added a message for right-click. Here is what the code looks like.
-----
CPoint mPointCurrent;
::GetCursorPos(&mPointCurrent);
CMenu mPopupMenu;
mPopupMenu.LoadMenu(IRD_CONTEXTMENU1);
// Program crashes at this point.
CMenu *pContextMenu = mPopupMenu.GetSubMenu(0);
pContextMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
mPointCurrent.x, mPointCurrent.y, GetActiveWindow(), NULL);
-----
The code above does not work. The program crashes when I right-click inside the dialog box. Is the code above for a view class only? Is there a different and corrent way to implement a context menu inside a dialog box?
Thanks,
Kuphryn
Re: Context Menu & Dialog Box :: MFC
It crashes you say.....
Are you sure that IRD_CONTEXTMENU1 actually represents your resource as when VC++ creates a menu it would usually call this IDR_CONTEXTMENU1
I tried the following and it worked ok
Code:
void CTestKuphDlg::OnRButtonUp(UINT nFlags, CPoint point)
{
CPoint mPointCurrent;
CMenu mPopupMenu;
mPopupMenu.LoadMenu(IRD_CONTEXTMENU1);
ClientToScreen(&point);
CMenu *pContextMenu = mPopupMenu.GetSubMenu(0);
pContextMenu->TrackPopupMenu(TPM_LEFTALIGN |
TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD |
TPM_NONOTIFY,point.x, point.y,GetActiveWindow(), NULL);
pContextMenu->DestroyMenu();
CDialog::OnRButtonUp(nFlags, point);
}