Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Context Menu & Dialog Box :: MFC

Context Menu & Dialog Box :: MFC

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionlearning
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    valikac
    wrote on last edited by
    #1

    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

    J S V 3 Replies Last reply
    0
    • V valikac

      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

      J Offline
      J Offline
      Jan Hirak
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • V valikac

        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

        S Offline
        S Offline
        Shog9 0
        wrote on last edited by
        #3

        I don't know. But, you can rule out a couple of things right off: put in an ASSERT() for pContextMenu, and for GetActiveWindow(). 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...

        1 Reply Last reply
        0
        • J Jan Hirak

          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

          S Offline
          S Offline
          Shog9 0
          wrote on last edited by
          #4

          No, ::GetCursorPos() returns screen coordinates, and that's what TrackPopupMenu() expects. I do wonder if maybe having both TPM_LEFTBUTTON and TPM_RIGHTBUTTON set could be causing problems though...

          ---

          Shog9 If I could sleep forever, I could forget about everything...

          1 Reply Last reply
          0
          • V valikac

            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

            V Offline
            V Offline
            valikac
            wrote on last edited by
            #5

            Okay. Thanks. I found the problem. // The menu ID was incorrect ----- CMenu mPopupMenu; mPopupMenu.LoadMenu(IRD_CORRECTMENUID); ----- Kuphryn

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups