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. Popup Menus

Popup Menus

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
4 Posts 4 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.
  • M Offline
    M Offline
    Marissa182
    wrote on last edited by
    #1

    Hi im creating a popup menu but i only want it to show up when the user right clicks on a tree ctrl in a dialog i mapped the tree controls onrightclick but i dont know how to get the mouse coordinates what is the best way to handle a popup menu for a tree control within a dialog

    H A 2 Replies Last reply
    0
    • M Marissa182

      Hi im creating a popup menu but i only want it to show up when the user right clicks on a tree ctrl in a dialog i mapped the tree controls onrightclick but i dont know how to get the mouse coordinates what is the best way to handle a popup menu for a tree control within a dialog

      H Offline
      H Offline
      HPSI
      wrote on last edited by
      #2

      It would be better to use WM_CONTEXTMENU instead of the right-click. The reason for this is that OnContextMenu() also catches Shift+F10, which is the Windows standard shortcut key for context menus (try it in the Explorer). But to catch right-clicks with OnContextMenu(), you also have to have a handler for ON_WM_RBUTTONDOWN - otherwise, the tree control will think you are starting a drag operation. One final thing: if OnContextMenu gets called because of Shift+F10, the point will always be -1, -1. Therefore, always use GetCursorPos() to get the actual point. Here is an example (it assumes you have derived a minimal CMyTreeCtrl from CTreeCtrl):

      void CMyTreeCtrl::OnRButtonDown(UINT nFlags, CPoint point)
      {
      // disable this message to allow OnContextMenu to work

      //CTreeCtrl::OnRButtonDown(nFlags, point);
      

      }

      void CMyTreeCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
      {
      CPoint pos;
      GetCursorPos(&pos);
      ScreenToClient(&pos);

      UINT flags = 0;
      HTREEITEM hItem = HitTest(pos, &flags);
      
      SetFocus();
      if (hItem != NULL)
      {
          SelectItem(hItem);
      
          CMenu menu;
          menu.CreatePopupMenu();
      
          VERIFY(menu.AppendMenu(MF\_STRING, ID\_MYID, "My Selection"));
      
          // display the menu
          GetCursorPos(&pos);        // need screen coordinates
          VERIFY(menu.TrackPopupMenu(TPM\_LEFTALIGN | TPM\_LEFTBUTTON, pos.x, pos.y, this));
      }
      else
      {
          TRACE("not on item\\n");
      }
      

      }

      HPS HwndSpy - GUI developer's aid to visually locate and inspect windows. For the month of August only, use coupon code CP-81239 for 30% off.

      S 1 Reply Last reply
      0
      • M Marissa182

        Hi im creating a popup menu but i only want it to show up when the user right clicks on a tree ctrl in a dialog i mapped the tree controls onrightclick but i dont know how to get the mouse coordinates what is the best way to handle a popup menu for a tree control within a dialog

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        void CTestView::OnContextMenu(CWnd* pWnd, CPoint point) { CMenu *pMenu,oMenu; oMenu.LoadMenu(IDR_TESTMFTYPE); pMenu = oMenu.GetSubMenu(0); pMenu->EnableMenuItem(ID_FILE_NEW,MF_DISABLED | MF_GRAYED); pMenu->TrackPopupMenu(0,point.x,point.y,this); }

        1 Reply Last reply
        0
        • H HPSI

          It would be better to use WM_CONTEXTMENU instead of the right-click. The reason for this is that OnContextMenu() also catches Shift+F10, which is the Windows standard shortcut key for context menus (try it in the Explorer). But to catch right-clicks with OnContextMenu(), you also have to have a handler for ON_WM_RBUTTONDOWN - otherwise, the tree control will think you are starting a drag operation. One final thing: if OnContextMenu gets called because of Shift+F10, the point will always be -1, -1. Therefore, always use GetCursorPos() to get the actual point. Here is an example (it assumes you have derived a minimal CMyTreeCtrl from CTreeCtrl):

          void CMyTreeCtrl::OnRButtonDown(UINT nFlags, CPoint point)
          {
          // disable this message to allow OnContextMenu to work

          //CTreeCtrl::OnRButtonDown(nFlags, point);
          

          }

          void CMyTreeCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
          {
          CPoint pos;
          GetCursorPos(&pos);
          ScreenToClient(&pos);

          UINT flags = 0;
          HTREEITEM hItem = HitTest(pos, &flags);
          
          SetFocus();
          if (hItem != NULL)
          {
              SelectItem(hItem);
          
              CMenu menu;
              menu.CreatePopupMenu();
          
              VERIFY(menu.AppendMenu(MF\_STRING, ID\_MYID, "My Selection"));
          
              // display the menu
              GetCursorPos(&pos);        // need screen coordinates
              VERIFY(menu.TrackPopupMenu(TPM\_LEFTALIGN | TPM\_LEFTBUTTON, pos.x, pos.y, this));
          }
          else
          {
              TRACE("not on item\\n");
          }
          

          }

          HPS HwndSpy - GUI developer's aid to visually locate and inspect windows. For the month of August only, use coupon code CP-81239 for 30% off.

          S Offline
          S Offline
          Snillet2k
          wrote on last edited by
          #4

          I readed this topic and I tested it. HitTest() and SelectItem() dosen't exist when i try to complie it. Do I have to include header files or something?

          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