CtreeCtrl Popupmenu
-
Hi all, i have a tree control in which i want to show the popup menu when someone right clicks on item. so that i hadled the NM_RCLICK message but the pop up menu comes somewhere near start button!! the code is as follows void CTreeViewDlg::OnNMRclickTree1(NMHDR *pNMHDR, LRESULT *pResult) { NM_TREEVIEW* pNmTreeVew = NULL; pNmTreeVew =(NM_TREEVIEW*) pNMHDR; if(pNmTreeVew) { CPoint point; point.x = pNmTreeVew->ptDrag.x; point.y = pNmTreeVew->ptDrag.y;/* HMENU hMenu = LoadMenu(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MENU1)); if(hMenu) { TrackPopupMenu(hMenu,0,point.x,point.y,0,pTreeCtrl->m_hWnd,NULL); } } } What can be the problem with this code? actually when i debugged the code it point.y shows some big value so please tell what is the problem Thanks and regards Harshal
-
Hi all, i have a tree control in which i want to show the popup menu when someone right clicks on item. so that i hadled the NM_RCLICK message but the pop up menu comes somewhere near start button!! the code is as follows void CTreeViewDlg::OnNMRclickTree1(NMHDR *pNMHDR, LRESULT *pResult) { NM_TREEVIEW* pNmTreeVew = NULL; pNmTreeVew =(NM_TREEVIEW*) pNMHDR; if(pNmTreeVew) { CPoint point; point.x = pNmTreeVew->ptDrag.x; point.y = pNmTreeVew->ptDrag.y;/* HMENU hMenu = LoadMenu(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MENU1)); if(hMenu) { TrackPopupMenu(hMenu,0,point.x,point.y,0,pTreeCtrl->m_hWnd,NULL); } } } What can be the problem with this code? actually when i debugged the code it point.y shows some big value so please tell what is the problem Thanks and regards Harshal
-
Hi, it retrieves you coordinates for the screen and you need convert it to client. Use ScreenToClient method of treectrl: void ScreenToClient( LPPOINT lpPoint ) const;
----------- Mila
I have done it this way but still it shows the menu at same position.
-
Hi all, i have a tree control in which i want to show the popup menu when someone right clicks on item. so that i hadled the NM_RCLICK message but the pop up menu comes somewhere near start button!! the code is as follows void CTreeViewDlg::OnNMRclickTree1(NMHDR *pNMHDR, LRESULT *pResult) { NM_TREEVIEW* pNmTreeVew = NULL; pNmTreeVew =(NM_TREEVIEW*) pNMHDR; if(pNmTreeVew) { CPoint point; point.x = pNmTreeVew->ptDrag.x; point.y = pNmTreeVew->ptDrag.y;/* HMENU hMenu = LoadMenu(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MENU1)); if(hMenu) { TrackPopupMenu(hMenu,0,point.x,point.y,0,pTreeCtrl->m_hWnd,NULL); } } } What can be the problem with this code? actually when i debugged the code it point.y shows some big value so please tell what is the problem Thanks and regards Harshal
I think the
pNMHDR
argument of your handler actually does not point toNMTREEVIEW
structure. (See the documentation ofNM_RCLICK
in case of tree controls). But you can obtain the current cursor position usingGetCursorPos
function:CPoint point; GetCursorPosition(&point);
I hope this helps.
-
I have done it this way but still it shows the menu at same position.
-
Try GetMessagePos(): ... point = (CPoint) GetMessagePos(); tree->ScreenToClient(&point); In my case works fine.
----------- Mila
void CTreeViewDlg::OnNMRclickTree1(NMHDR *pNMHDR, LRESULT *pResult) { CPoint ptMouse; DWORD dwPos; UINT nFlags; CTreeCtrl *pTreeCtrl; pTreeCtrl = (CTreeCtrl *)GetDlgItem(IDC_TREE1); { dwPos = ::GetMessagePos(); ptMouse.x = LOWORD (dwPos); ptMouse.y = HIWORD (dwPos); CPoint ptAction = ptMouse; // convert coordinates pTreeCtrl->ScreenToClient(&ptAction); // determine if click is on tree item HTREEITEM hItemRClick = pTreeCtrl->HitTest(ptAction, &nFlags); // if click is on tree item, if (hItemRClick != NULL) { // set selection to right+clicked item pTreeCtrl->SelectItem(hItemRClick); HMENU hMenu = LoadMenu(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MENU1)); if(hMenu) { hMenu = GetSubMenu(hMenu,1); if(hMenu) { TrackPopupMenu(hMenu,0,ptMouse.x,ptMouse.y,0,pTreeCtrl->m_hWnd,NULL); } } } } *pResult = 0; } i have done something like this Noe the pop up menu is poping up but whenever i click on something it does not go to respective handler!! Thanks and regards Harshal