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:
<pre>void CLeftView::OnRClick(NMHDR* pNMHDR, LRESULT* pResult)
{
try
{
CPoint point(GetMessagePos());
int popupMenuXPos = point.x;
int popupMenuYPos = point.y;
ScreenToClient(&point);
CTreeCtrl& tree = GetTreeCtrl();
HTREEITEM hti = tree.HitTest(point);
if(hti)
{
tree.SelectItem(hti);
CString strTreeItem = tree.GetItemText(hti);
WorkspaceItemData* pWspItemData = new WorkspaceItemData;
pWspItemData = (WorkspaceItemData*)(tree.GetItemData(hti));
CString m_selectedTreeItemFilePath = pWspItemData->m_path;
HMENU menu = ::LoadMenu(::GetModuleHandle(0), MAKEINTRESOURCE(IDR_POPUP_TREEVIEW));
HMENU subMenu = ::GetSubMenu(menu, 0);
int command = ::TrackPopupMenu(subMenu,
TPM_RETURNCMD | TPM_LEFTBUTTON,
popupMenuXPos,
popupMenuYPos,
0,
AfxGetMainWnd()->GetSafeHwnd(), 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
<pre>ON_COMMAND(ID_OPEN_OPENFILE, &CLeftView::OnOpenOpenfile)
void CLeftView::OnOpenOpenfile()
{
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.