I have a CtreeView and I would like to associate a context menu to it when I do a right click on a element. I use Visual C++ 6 and I would like to know the easest way to do that.
Thank you
Printable View
I have a CtreeView and I would like to associate a context menu to it when I do a right click on a element. I use Visual C++ 6 and I would like to know the easest way to do that.
Thank you
Quote:
Originally Posted by Geof
if you would just have searched the forum for: "treeview context menu" first you would've been surprised by the outcome :rolleyes:
Anyway here's one:
or:Code:void CTreeCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
UINT uFlags;
CPoint pt(point);
ScreenToClient(&pt);
HTREEITEM htItem = HitTest(pt, &uFlags);
if (htItem == NULL || !(uFlags & TVHT_ONITEM)) // make sure we are on an item
return;
Select( htItem, TVGN_CARET);
m_htDropTarget = htItem;
CMenu *pMenu = m_contextMenu.GetSubMenu(0);
ASSERT(pMenu != NULL);
// handle anything that you need for your menus here
pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}
Code:CMyDialog::OnRclickTree(NMHDR*,LRESULT *pResult)
{
CPoint pt, ptree;
if (::GetCursorPos(&pt))
{
ptree = pt;
m_tree.ScreenToClient(&ptree);
m_submenu = 0;
UINT flags = 0;
HTREEITEM it = m_tree.HitTest(ptree,&flags);
if (it)
m_tree.Select(it,TVGN_CARET);
CMenu menu;
menu.LoadMenu(IDR_MY_MENU);
menu.GetSubMenu(0)->TrackPopupMenu(TPM_CENTERALIGN|TPM_RIGHTBUTTON,pt.x,pt.y,this);
menu.DestroyMenu();
*pResult = 0;
}
}
This one is prefered over the right click one, because a context menu doesn't always have to be shown when right clicking.Quote:
Originally Posted by Alin
The default behavior of the TreeView is to fire up the context menu on a double right click, however, which can be annoying.
Mm... I don't have that problem.Quote:
Originally Posted by JetDeveloper
I just tried it.
I create a new MFC dialog project.
Added a treeview to the dialog.
Added some items to the tree.
Added a OnContextMenu to the dialog.
and it works on single right clicking the treeview.
Code:void CTVTestDlg::OnContextMenu(CWnd* pWnd, CPoint /*point*/)
{
if (pWnd->GetDlgCtrlID() == IDC_TREE1)
AfxMessageBox("ok");
}
So how would you write a message handler or event hanlder for this menu now? Like a funciton that would get hit with what was just selected on the pop up?
Hello,
For CTreeView class, I also have observed this problem. Handlers for WM_CONTEXTMENU or WM_RBUTTONDOWN / WM_RBUTTONUP are called only when you double click the right mouse button, as if the the first click goes to select the item. What can be the reason? How to overcome that?
Regards.
Pravin.