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