用AppWizard生成了一个Explorer样式的SDI程序,分割条左边的视图类CLeftView的父类是CTreeView,右边的视图的父类是CFormView。
我的问题是:
左边的CTreeView中有两类节点
父节点1
 |
 |--子节点(若干个)
 |
父节点2
 |
父节点3
现在想在用鼠标右键单击父节点和子节点时显示不同的弹出菜单。
我在CLeftView中处理NM_RCLICK消息
void CLeftView::OnRclickTree(NMHDR* pNMHDR, LRESULT* pResult) 
{
// TODO: Add your control notification handler code here
HTREEITEM hItem ;
CPoint cPoint;
::GetCursorPos(&cPoint); CTreeCtrl &tree = GetTreeCtrl();
tree.ScreenToClient(&cPoint);
hItem = tree.HitTest(cPoint);
ASSERT(hItem != NULL); CMenu menuTree;
if (!menuTree.LoadMenu(IDR_MENU_TREE))
{
CMenu *pMenuTree = menuTree.GetSubMenu(0);
pMenuTree->TrackPopupMenu(TPM_LEFTALIGN, cPoint.x, cPoint.y, this);
}
else
AfxMessageBox("Failed to load menu"); *pResult = 0;
}
可是每次都会提示加载菜单资源IDR_MENU_TREE失败。
我看到大家以往处理上下文菜单都是在WM_CONTEXTMENU中,但是感觉在我的程序中处理WM_CONTEXTMENU并不能满足我的要求。
望大侠们赐教!

解决方案 »

  1.   

    void CTest6Dlg::OnRclickTree1(NMHDR* pNMHDR, LRESULT* pResult) { CPoint point;
    GetCursorPos(&point);
    CPoint pointTree = point;
    m_tree.ScreenToClient(&pointTree);
    HTREEITEM item;
    UINT flag = TVHT_ONITEM;
    item = m_tree.HitTest(pointTree, &flag);
    if ( item != NULL )
    {
    m_tree.SelectItem(item);
    CMenu menu;
    menu.LoadMenu(IDR_MENU1);
    CMenu* popup = menu.GetSubMenu(0); ASSERT( popup != NULL );
    popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this, NULL);
    } *pResult = 0;}
      

  2.   

    试过了楼上的代码,但是ASSERT宏过不去,调试发现menu.GetSubMenu(0)的返回值是NULL。
      

  3.   

    void CLeftView::OnRclickTree(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    // TODO: Add your control notification handler code here
    HTREEITEM hItem ;
    CPoint cPoint;
    ::GetCursorPos(&cPoint);CTreeCtrl &tree = GetTreeCtrl();
    tree.ScreenToClient(&cPoint);
    hItem = tree.HitTest(cPoint);
    ASSERT(hItem != NULL);CMenu menuTree;
    menuTree.LoadMenu(IDR_MENU_TREE);
    CMenu *pMenuTree = NULL;
    if (hItem.....)    根据hItem判断加载哪个菜单
    {
    menuTree.GetSubMenu(0);
    }
    else
    {
    ......
    }
    pMenuTree->TrackPopupMenu(TPM_LEFTALIGN, cPoint.x, cPoint.y, this);*pResult = 0;
    }