mouse position in treeview
-
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?
-
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?
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[^]
-
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?
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 alsoGetCursorPos()
to get mouse the position. But I prefer the first one. Also I think there is no need to callScreenToClient(&point)
.zeus_master wrote:
2>the add and del note command?
I didn't understand this nave
-
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 alsoGetCursorPos()
to get mouse the position. But I prefer the first one. Also I think there is no need to callScreenToClient(&point)
.zeus_master wrote:
2>the add and del note command?
I didn't understand this nave
Naveen R wrote:
But I prefer the first one
Why??
Nibu thomas A Developer Programming tips[^] My site[^]
-
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 alsoGetCursorPos()
to get mouse the position. But I prefer the first one. Also I think there is no need to callScreenToClient(&point)
.zeus_master wrote:
2>the add and del note command?
I didn't understand this nave
Million thanks! 2> I mean how to add or delete the tree notes?
-
Naveen R wrote:
But I prefer the first one
Why??
Nibu thomas A Developer Programming tips[^] My site[^]
-
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
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[^]
-
Million thanks! 2> I mean how to add or delete the tree notes?
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
-
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
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
-
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
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
-
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
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.
-
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.
-
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
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: