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
-
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
-
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
I don't know. But, you can rule out a couple of things right off: put in an
ASSERT()
forpContextMenu
, and forGetActiveWindow()
. If either one triggers, there's your problem:CMenu *pContextMenu = mPopupMenu.GetSubMenu(0);
ASSERT(NULL != pContextMenu);
ASSERT(NULL != GetActiveWindow());---
Shog9 If I could sleep forever, I could forget about everything...
-
I thing your problem is that you are passing argument &mPointCurrent in client coordinates. You need to implement function ClientToScreen()and converse it into screen coordinates. ClientToScreen(&mPointCurrent);//after mPopupMenu.LoadMenu
No,
::GetCursorPos()
returns screen coordinates, and that's whatTrackPopupMenu()
expects. I do wonder if maybe having bothTPM_LEFTBUTTON
andTPM_RIGHTBUTTON
set could be causing problems though...---
Shog9 If I could sleep forever, I could forget about everything...
-
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