Thread: TreeView + Popup menu

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    TreeView + Popup menu

    A while ago I got this tree view working just fine, but now I want to add a popup menu to it, and there's a problem. At first, I though handling the WM_RBUTTONDOWN message would work, but the tree view, not the parent window, receives mouse input, so I don't ever receive this message.

    Then I thought, oh yeah I'll handle the NM_RCLICK notification, which works fine except I also need to somehow position the popup menu right next to the mouse, and NM_RCLICK apparently doesn't include the cursor position. So I've got:
    Code:
    case WM_NOTIFY: //handle treeview messages
        {
          if(wParam==ID_BROWSE_TREE)
          {
            LPNMHDR nm = (LPNMHDR)lParam;
            switch (nm->code)
            {
              case NM_RCLICK:
              {
                static HMENU hMenu = LoadMenu(GetModuleHandle(NULL),MAKEINTRESOURCE(IDR_MENU2));
                if (!hMenu)
               {
                 MessageBox(NULL,"Unable to load popup menu!","Error",MB_OK);
                 return;
               }
               TrackPopupMenuEx(GetSubMenu(hMenu,0),TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON,0,0,hTree,NULL); //how do I get these two args?
              }
              break;
    Any ideas on how I should/can do popup menus for a tree view?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Maybe GetMessagePos would work.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Ah parfait!

    And here I am looking through the mouse input functions for a solution
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Also, look into the WM_CONTEXTMENU notification.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    That sounds like slightly better solution, but I just encountered another problem which I doubt it will fix. When you right click an item in the tree view, apparently it doesn't select it, instead it just highlights the item. So now I can't find any way to get a handle to the item that a user right clicks (unless they left-click first). Another simple solution please
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Look into TreeView_HitTest. What I did a while ago was catch the NM_RCLICK message, retrieve the mouse position (GetCursorPos), convert to local coordinates (ScreenToClient) then call TreeView_HitTest with the proper parameters to retrieve the treeview item at that location (or NULL if none exists).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Send the TVM_HITTEST message to see what node (if any) is at the specified coordinates.

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Hm well I tried TreeView_HitTest, but I just realized I was using the wrong cursor position (when I click the popup menu item). I'll try doing things a little differently and see I that works...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Thanks magos now it seems to be working perfectly (stay tuned for further problems )
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I use the right click as a selection button as well and then fire off the right click menu for that item. Ran into the same problem as you in that the right mouse button does not select the item it's over.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. popup menu in MFC?
    By Robert602 in forum Windows Programming
    Replies: 7
    Last Post: 07-25-2007, 09:34 PM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM