Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an SDI MFC explorer style application. It has a tree view on the left and an empty pane on the right with a splitter bar. I am trying to get a pop up menu to work when the user clicks on an item in the tree view.

I can successfully detect when the user has clicked on a tree-view item and the pop up menu is displayed and is not greyed out. This is my code:

C++
<pre>void CLeftView::OnRClick(NMHDR* pNMHDR, LRESULT* pResult)
{
	try
	{
		//get the position of the cursor when the last message was retrieved using GetMessage
		CPoint point(GetMessagePos());
		//jl
		int popupMenuXPos = point.x;
		int popupMenuYPos = point.y;
		//converts the screen coords of a point on the display to client coords which are relative
		//to the upper left hand corner of the CWnd client area
		ScreenToClient(&point);
		   
		CTreeCtrl& tree = GetTreeCtrl();
	   
		//tests 'point' to see if there is a tree-ctrl item there. If not, it returns null. If there is, 
		//it returns a handle to it
		HTREEITEM hti = tree.HitTest(point);
		if(hti)
		{
			//selects the item
			tree.SelectItem(hti);
			//test - to see what is at that position
			CString strTreeItem = tree.GetItemText(hti);
		  
			//get at the data held with the tree-view item
			WorkspaceItemData* pWspItemData = new WorkspaceItemData;
			pWspItemData = (WorkspaceItemData*)(tree.GetItemData(hti));
					
			//get the tree item's path
			CString m_selectedTreeItemFilePath = pWspItemData->m_path;
			// load popup menu
			HMENU menu = ::LoadMenu(::GetModuleHandle(0), MAKEINTRESOURCE(IDR_POPUP_TREEVIEW/*IDR_MENU1*/));
			HMENU subMenu = ::GetSubMenu(menu, 0);
			int command = ::TrackPopupMenu(subMenu,
				TPM_RETURNCMD | TPM_LEFTBUTTON,
				popupMenuXPos,
				popupMenuYPos,
				0,
				AfxGetMainWnd()->GetSafeHwnd()/*parentHandle*/, 0);

		}
	}
	catch( ... )
	{
		AfxMessageBox(_T("CLeftView::OnRClick"));
	}
  
}


I created the popup menu in the resource editor and the open menu item has ID = ID_OPEN_OPENFILE. In LeftView.cpp
C++
<pre>ON_COMMAND(ID_OPEN_OPENFILE, &CLeftView::OnOpenOpenfile)

C++
void CLeftView::OnOpenOpenfile()
{
	// TODO: Add your command handler code here
	int x=2;
}


However, when I run the application, and right-click on a tree-view item, the pop-up menu appears and is not greyed out so it seems to know it has a message handler (as it was greyed out before I added the OnOpenOpenfile method), but when I left-clik on the 'open' menu item the event handler is not called (I have a break-point in it so i know).

Please could somebody tell me what I am missing.
Posted

1 solution

Try doing the TrackPopupMenu call without the TPM_RETURNCMD. Also provide the handle to your CTreeView window. Like:

C++
::TrackPopupMenu(subMenu,
    TPM_LEFTBUTTON,
    popupMenuXPos,
    popupMenuYPos,
    0,
    GetSafeHwnd()/*parentHandle*/, 0);
 
Share this answer
 
Comments
Jackie Lloyd 6-Oct-11 5:32am    
Thankyou so much - you are fantastic, don't know what I'd do without help like this. I think I can see now that I had given a handle to the main window rather than the tree-view which is really the parent of the pop-up menu, and was the TPM_RETURNCMD stopping the ID_OPEN_OPENFILE message being routed to the left view? Is my understanding correct?
bolivar123 7-Oct-11 0:07am    
You're welcome. You pass in the handle of the window that has the message handlers for your popup menu. I noticed by the message map macros you have the handler in your CTreeview window. That is correct the TPM_RETURNCMD flag causes the TrackPopupMenu function to hold modal until the user selects an item or dismisses the popup menu. The result of the TrackPopupMenu function call with the TPM_RETURNCMD flag is the command ID of the menu item the user chose.
Jackie Lloyd 7-Oct-11 3:05am    
Thankyou again, I think I understand now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900