|
-
December 24th, 2004, 02:39 PM
#1
Creating Context menu
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
-
December 24th, 2004, 03:06 PM
#2
Re: Creating Context menu
 Originally Posted by Geof
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
if you would just have searched the forum for: "treeview context menu" first you would've been surprised by the outcome
Anyway here's one:
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);
}
or:
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;
}
}
-
December 25th, 2004, 03:55 AM
#3
Re: Creating Context menu
 Originally Posted by Alin
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);
}
This one is prefered over the right click one, because a context menu doesn't always have to be shown when right clicking.
-
December 25th, 2004, 01:04 PM
#4
Re: Creating Context menu
The default behavior of the TreeView is to fire up the context menu on a double right click, however, which can be annoying.
-
December 26th, 2004, 04:06 AM
#5
Re: Creating Context menu
 Originally Posted by JetDeveloper
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.
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");
}
-
June 13th, 2006, 01:07 PM
#6
Re: Creating Context menu
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?
-
June 14th, 2006, 01:30 AM
#7
Re: Creating Context menu
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|