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. mouse position in treeview

mouse position in treeview

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondata-structures
13 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.
  • Z Offline
    Z Offline
    zeus_master
    wrote on last edited by
    #1

    Dear VC-masters; A simple problem need your help. a treeview built in a toolbar in mutil-doc project. while right key happen on the tree notes,I want to creat a pop-menu to operate the treeview, such as add note or del note in the tree. void CMainFrame::OnRclickTree(NMHDR* pNMHDR, LRESULT* pResult) { CMenu dMenu; if(!dMenu.LoadMenu(IDR_TREERKEY)) { AfxThrowResourceException(); } CMenu *pPopupMenu = dMenu.GetSubMenu(0); ASSERT(pPopupMenu != NULL); CPoint point; ScreenToClient(&point):confused:; :doh: pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON,point.x,point.y,AfxGetMainWnd()); } 1>how can I get current mouse position? 2>the add and del note command?

    N N 2 Replies Last reply
    0
    • Z zeus_master

      Dear VC-masters; A simple problem need your help. a treeview built in a toolbar in mutil-doc project. while right key happen on the tree notes,I want to creat a pop-menu to operate the treeview, such as add note or del note in the tree. void CMainFrame::OnRclickTree(NMHDR* pNMHDR, LRESULT* pResult) { CMenu dMenu; if(!dMenu.LoadMenu(IDR_TREERKEY)) { AfxThrowResourceException(); } CMenu *pPopupMenu = dMenu.GetSubMenu(0); ASSERT(pPopupMenu != NULL); CPoint point; ScreenToClient(&point):confused:; :doh: pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON,point.x,point.y,AfxGetMainWnd()); } 1>how can I get current mouse position? 2>the add and del note command?

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      zeus_master wrote:

      1>how can I get current mouse position?

      ::GetCursorPos(LPPOINT)

      The cursor position is always specified in screen coordinates so use ScreenToClient to convert to client coordinates.


      Nibu thomas A Developer Programming tips[^]  My site[^]

      1 Reply Last reply
      0
      • Z zeus_master

        Dear VC-masters; A simple problem need your help. a treeview built in a toolbar in mutil-doc project. while right key happen on the tree notes,I want to creat a pop-menu to operate the treeview, such as add note or del note in the tree. void CMainFrame::OnRclickTree(NMHDR* pNMHDR, LRESULT* pResult) { CMenu dMenu; if(!dMenu.LoadMenu(IDR_TREERKEY)) { AfxThrowResourceException(); } CMenu *pPopupMenu = dMenu.GetSubMenu(0); ASSERT(pPopupMenu != NULL); CPoint point; ScreenToClient(&point):confused:; :doh: pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON,point.x,point.y,AfxGetMainWnd()); } 1>how can I get current mouse position? 2>the add and del note command?

        N Offline
        N Offline
        Naveen
        wrote on last edited by
        #3

        zeus_master wrote:

        1>how can I get current mouse position?

        use GetCurrentMessage() funtion. it will return a structure of MSG. The MSG contains a member that holds the point in which the last message occured. U can also GetCursorPos() to get mouse the position. But I prefer the first one. Also I think there is no need to call ScreenToClient(&point).

        zeus_master wrote:

        2>the add and del note command?

        I didn't understand this nave

        N Z 2 Replies Last reply
        0
        • N Naveen

          zeus_master wrote:

          1>how can I get current mouse position?

          use GetCurrentMessage() funtion. it will return a structure of MSG. The MSG contains a member that holds the point in which the last message occured. U can also GetCursorPos() to get mouse the position. But I prefer the first one. Also I think there is no need to call ScreenToClient(&point).

          zeus_master wrote:

          2>the add and del note command?

          I didn't understand this nave

          N Offline
          N Offline
          Nibu babu thomas
          wrote on last edited by
          #4

          Naveen R wrote:

          But I prefer the first one

          Why??


          Nibu thomas A Developer Programming tips[^]  My site[^]

          N 1 Reply Last reply
          0
          • N Naveen

            zeus_master wrote:

            1>how can I get current mouse position?

            use GetCurrentMessage() funtion. it will return a structure of MSG. The MSG contains a member that holds the point in which the last message occured. U can also GetCursorPos() to get mouse the position. But I prefer the first one. Also I think there is no need to call ScreenToClient(&point).

            zeus_master wrote:

            2>the add and del note command?

            I didn't understand this nave

            Z Offline
            Z Offline
            zeus_master
            wrote on last edited by
            #5

            Million thanks! 2> I mean how to add or delete the tree notes?

            N 1 Reply Last reply
            0
            • N Nibu babu thomas

              Naveen R wrote:

              But I prefer the first one

              Why??


              Nibu thomas A Developer Programming tips[^]  My site[^]

              N Offline
              N Offline
              Naveen
              wrote on last edited by
              #6

              suppose u have right clicked the mouse while the mouse is moving fast. By the u call time GetCursorPos() the mouse might have moved to some another location or even over some other window. So showing popup at that point make no sense. :) do u agree? nave

              N 1 Reply Last reply
              0
              • N Naveen

                suppose u have right clicked the mouse while the mouse is moving fast. By the u call time GetCursorPos() the mouse might have moved to some another location or even over some other window. So showing popup at that point make no sense. :) do u agree? nave

                N Offline
                N Offline
                Nibu babu thomas
                wrote on last edited by
                #7

                Naveen R wrote:

                do u agree?

                Yeah I do. I see that with tray applications. Click on the tray icon and move your mouse. The menu comes up where your mouse pointer is. But since I am lazy :-> I like this. It saves me the time and effort to move down and click on the menu there.;)


                Nibu thomas A Developer Programming tips[^]  My site[^]

                1 Reply Last reply
                0
                • Z zeus_master

                  Million thanks! 2> I mean how to add or delete the tree notes?

                  N Offline
                  N Offline
                  Naveen
                  wrote on last edited by
                  #8

                  so what i understood is, u want to show some popup menu will item Add and Delete. When u click Delete button u and u want to delete the selected item.. 1) call GetSelectedItem( ); -> it will return and handle to the selected item 2) the call the DeleteItem(); with the handle returned by the GetSelectedItem() now to add a child node under the seleted node 3. Call InsertItem( LPCTSTR lpszItem, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter ); with hParent as the handle returned by GetSelectedItem(); nave

                  Z 1 Reply Last reply
                  0
                  • N Naveen

                    so what i understood is, u want to show some popup menu will item Add and Delete. When u click Delete button u and u want to delete the selected item.. 1) call GetSelectedItem( ); -> it will return and handle to the selected item 2) the call the DeleteItem(); with the handle returned by the GetSelectedItem() now to add a child node under the seleted node 3. Call InsertItem( LPCTSTR lpszItem, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter ); with hParent as the handle returned by GetSelectedItem(); nave

                    Z Offline
                    Z Offline
                    zeus_master
                    wrote on last edited by
                    #9

                    I'v tried them, thank you. but there still a problem while insert Items. +ROOT1 | -ROOT2 |_child1 |_child2 |_child3 first, LClick set sel on child1; then move mouse to child2,RClick child2, Pop-menu appears on child2,it is normal/OK. 1>but the focus still at stay at child1, whill click pop-menu add note command , a new child note will be created under child1 ,but not the position RClick occurs and not the position pop-menu on( should be child2). void CMainFrame::OnInsertItem() { // TODO: Add your command handler code here m_Tree.InsertItem("new node",**m_Tree.GetSelectedItem(),**TVI_LAST);:confused: m_wndTree.UpdateWindow(); // it seems useless } 2>and the new child note will be not visible/enable at once, I need LClick the ROOT2 or other notes, the new created note can be visible. if I insert the items based on ROOT note, it can be refresh and can be seen right away . void CMainFrame::OnInsertROOTItem() { // TODO: Add your command handler code here m_Tree.InsertItem("New note",TVI_ROOT); } -- modified at 3:50 Thursday 15th June, 2006

                    N 1 Reply Last reply
                    0
                    • Z zeus_master

                      I'v tried them, thank you. but there still a problem while insert Items. +ROOT1 | -ROOT2 |_child1 |_child2 |_child3 first, LClick set sel on child1; then move mouse to child2,RClick child2, Pop-menu appears on child2,it is normal/OK. 1>but the focus still at stay at child1, whill click pop-menu add note command , a new child note will be created under child1 ,but not the position RClick occurs and not the position pop-menu on( should be child2). void CMainFrame::OnInsertItem() { // TODO: Add your command handler code here m_Tree.InsertItem("new node",**m_Tree.GetSelectedItem(),**TVI_LAST);:confused: m_wndTree.UpdateWindow(); // it seems useless } 2>and the new child note will be not visible/enable at once, I need LClick the ROOT2 or other notes, the new created note can be visible. if I insert the items based on ROOT note, it can be refresh and can be seen right away . void CMainFrame::OnInsertROOTItem() { // TODO: Add your command handler code here m_Tree.InsertItem("New note",TVI_ROOT); } -- modified at 3:50 Thursday 15th June, 2006

                      N Offline
                      N Offline
                      Naveen
                      wrote on last edited by
                      #10

                      zeus_master wrote:

                      1>but the focus still at stay at child1,

                      in that case u should explicitly set the focus to child2. The HitTest() function will help u in this 1. Take the mouse point 2. Call HitTest funtion with that point( this will retun the handle to the item below that point ) 3. Call SetItemState() for this item 4. Now the underling item will be selected. after this u show the popup menu...:)

                      zeus_master wrote:

                      2>and the new child note will be not visible/enable at once,

                      did u tried EnsureVisible() funtion? nave

                      Z 1 Reply Last reply
                      0
                      • N Naveen

                        zeus_master wrote:

                        1>but the focus still at stay at child1,

                        in that case u should explicitly set the focus to child2. The HitTest() function will help u in this 1. Take the mouse point 2. Call HitTest funtion with that point( this will retun the handle to the item below that point ) 3. Call SetItemState() for this item 4. Now the underling item will be selected. after this u show the popup menu...:)

                        zeus_master wrote:

                        2>and the new child note will be not visible/enable at once,

                        did u tried EnsureVisible() funtion? nave

                        Z Offline
                        Z Offline
                        zeus_master
                        wrote on last edited by
                        #11

                        void CMainFrame::OnRclickTree(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here CMenu dMenu; if(!dMenu.LoadMenu(IDR_TREERKEY)) { AfxThrowResourceException(); } CMenu *pPopupMenu = dMenu.GetSubMenu(0); ASSERT(pPopupMenu != NULL); CPoint point; ::GetCursorPos(&point); /* here the point still stay at where LClick set sel */ // const MSG * pMsg; // pMsg = GetCurrentMessage(); // point = pMsg->pt; HTREEITEM pitem; pitem=m_Tree.HitTest(point); m_Tree.SetItemState(pitem,TVIS_SELECTED,NULL); pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON,point.x,point.y,AfxGetMainWnd()); *pResult = 0; } I faild, set breakpoint and found that, the point still keep on the place where LClick set sel tree item, and the RClick at another item seems can't return mouse point at once. but what strangeness is the pop-menu can appear at where the RClick point/item. is the code wrong? appreciate for your kindly help.

                        N 1 Reply Last reply
                        0
                        • Z zeus_master

                          void CMainFrame::OnRclickTree(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here CMenu dMenu; if(!dMenu.LoadMenu(IDR_TREERKEY)) { AfxThrowResourceException(); } CMenu *pPopupMenu = dMenu.GetSubMenu(0); ASSERT(pPopupMenu != NULL); CPoint point; ::GetCursorPos(&point); /* here the point still stay at where LClick set sel */ // const MSG * pMsg; // pMsg = GetCurrentMessage(); // point = pMsg->pt; HTREEITEM pitem; pitem=m_Tree.HitTest(point); m_Tree.SetItemState(pitem,TVIS_SELECTED,NULL); pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON,point.x,point.y,AfxGetMainWnd()); *pResult = 0; } I faild, set breakpoint and found that, the point still keep on the place where LClick set sel tree item, and the RClick at another item seems can't return mouse point at once. but what strangeness is the pop-menu can appear at where the RClick point/item. is the code wrong? appreciate for your kindly help.

                          N Offline
                          N Offline
                          Naveen
                          wrote on last edited by
                          #12

                          zeus_master wrote:

                          pitem=m_Tree.HitTest(point);

                          there is problem here before u make the hittest do like this CPoint Pt = point ScreenToClient( Pt ); pitem = m_Tree.HitTest(Pt); nave

                          Z 1 Reply Last reply
                          0
                          • N Naveen

                            zeus_master wrote:

                            pitem=m_Tree.HitTest(point);

                            there is problem here before u make the hittest do like this CPoint Pt = point ScreenToClient( Pt ); pitem = m_Tree.HitTest(Pt); nave

                            Z Offline
                            Z Offline
                            zeus_master
                            wrote on last edited by
                            #13

                            it still didn't work. I debuged the code and found that calling ::GetCursorPos(&point); in RClick function, still return the point set by LClick. :sigh:

                            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