CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2001
    Posts
    81

    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

  2. #2
    Join Date
    Feb 2002
    Posts
    3,788

    Re: Creating Context menu

    Quote 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;
    	}
    }

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Creating Context menu

    Quote 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.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Aug 2004
    Location
    Land of sunshine and June Gloom
    Posts
    171

    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.

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Creating Context menu

    Quote 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");
    }
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  6. #6
    Join Date
    Jan 2005
    Posts
    248

    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?

  7. #7
    Join Date
    Feb 2000
    Location
    Indore, India
    Posts
    1,046

    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.
    Let me know if I have helped by rating this post

    Recent FAQs

    Drag an image
    Area of a window exposed on desktop
    Display rotated bitmap

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured