CTreeView的右键/左键的消息响应GetiIem Point - 小众知识

CTreeView的右键/左键的消息响应GetiIem Point

2015-11-26 08:17:42 苏内容
  标签: CTreeView/消息
阅读:5508

ON_NOTIFY_REFLECT(NM_RCLICK, &CLeftView::OnNMRClick)


NMHDR *pNMHDR 与  TVN_SELCHANGED消息做突破口呢

 

但是,有俩问题啊!
 
1.创建树之后,不知道为什么 第一个节点默认是可用状态的,这样我再次右击第一个节点 就卡了。
 
2.由于是不同节点,弹出不同的右键菜单,右击之后有几个函数获取不了值。item一直是0,不知道为什么只有单击节点之后,item才会好用
ItemSelect = pTree->GetSelectedItem();
HTREEITEM item = NULL;
int nParentCount = 0;
//判断选中节点有多少个父节点
item = pTree->GetParentItem(ItemSelect);
while (item!=NULL)
{
   nParentCount++;
   item = pTree->GetParentItem(item);
}  


void CLeftView::OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult)
{
    
    NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
    CTreeCtrl *pTree=&GetTreeCtrl();
    CPoint point;
    GetCursorPos(&point);
    ::SendMessage(pTree->m_hWnd,WM_RBUTTONDOWN,MK_RBUTTON|MK_CONTROL,MAKELPARAM(point.x,point.y)); 
    //CTreeView::OnLButtonDown(nFlags, point);
    // TODO: 在此添加控件通知处理程序代码
 
    //if(pNMTreeView->hdr.code==NM_RCLICK)
    //if()
    //{
    //::SendMessageA();
    ItemSelect = pTree->GetSelectedItem();
    HTREEITEM item = NULL;
    int nParentCount = 0;
//判断选中节点有多少个父节点
    item = pTree->GetParentItem(ItemSelect);
    while (item!=NULL)
    {
        nParentCount++;
        item = pTree->GetParentItem(item);
    }
//根据父节点的个数判断选中节点的右键菜单显示
        
        
        if (nParentCount == 3)
        {
            CMenu menu;
            menu.LoadMenu(IDR_MENU_ROUTE);
            if (g_shUserType != 1)
            {
                menu.EnableMenuItem(ID_MENU_ROUTE_DL, MF_GRAYED);
                menu.EnableMenuItem(ID_MENU_ROUTE_ED, MF_GRAYED);
            }
            menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);
        }
        if (nParentCount==2)
        {
            CMenu menu;
            menu.LoadMenu(IDR_MENU_MS);
            if (g_shUserType != 1)
            {
                menu.EnableMenuItem(ID_MENU_ROUTE_ADD, MF_GRAYED);
            }
            menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);
        }
        if(nParentCount==1)
        {
            CMenu menu;
        }
//    }
    *pResult = 0;
}

 


void CHistoryView::OnContextMenu(CWnd* pWnd, CPoint point)
{
    int tabindex=m_wndTabs.GetActiveTab();
    CString TabLable;
    m_wndTabs.GetTabLabel(tabindex,TabLable);
    CTreeCtrl* pWndTree;
    if(TabLable==TASKVIEW)
        pWndTree = (CTreeCtrl*) &m_wndFileView;
    else
        pWndTree= (CTreeCtrl*) &m_wndFunctionView;
    ASSERT_VALID(pWndTree);
 
    if (pWnd != /*pWndTree*/&m_wndTabs)
    {
//        CDockablePane::OnContextMenu(pWnd, point);
        return;
    }
 
    if (point != CPoint(-1, -1))
    {
        // Select clicked item:
        CPoint ptTree = point;
        pWndTree->ScreenToClient(&ptTree);
 
        UINT flags = 0;
        HTREEITEM hTreeItem = pWndTree->HitTest(ptTree, &flags);
        if (hTreeItem != NULL)
        {  
            //root can not be chosed..by ckf76528
            pWndTree->SelectItem(hTreeItem);
            if(hTreeItem == pWndTree->GetRootItem())
            {
                return;
            }
            
            hCurrentTreeItem = hTreeItem;
        }
        else
        {
            return;
        }
    }
    
    pWndTree->SetFocus();
 
    HMENU hMenu=LoadMenu(theApp.m_hInstance,MAKEINTRESOURCE(IDR_POPUP_EXPLORER));
    CMenu contextMenu;
    contextMenu.Attach(hMenu);
    if(TabLable==TASKVIEW)
    {
        CtrlContextMenuStatus();
        CMenu *pContextMenu=contextMenu.GetSubMenu(0);
        theApp.GetContextMenuManager()->ShowPopupMenu(pContextMenu->GetSafeHmenu()/*IDR_POPUP_EXPLORER*/, point.x, point.y, this, TRUE);
    }
    else
    {
        CMenu *pContextMenu=contextMenu.GetSubMenu(1);
        theApp.GetContextMenuManager()->ShowPopupMenu(pContextMenu->GetSafeHmenu(), point.x, point.y, this, TRUE);
    }
}

现象在tree的item上按鼠标左键无任何反应。

Lbutton 响应函数为:

void treeview::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    
    
    CPoint pt (point);
    ClientToScreen (&pt);
    CWnd* pDropWnd = WindowFromPoint (pt);
    UINT uFlags;
    // get the item that is below cursor
    m_hDropItem = ((CTreeCtrl*)pDropWnd)->HitTest (pt, &uFlags);
    // highlight it
    ((CTreeCtrl*)pDropWnd)->SelectDropTarget (m_hDropItem);
    keyset dlg;
    dlg.DoModal();
    CView::OnLButtonDown(nFlags, point);
}

我创建了一个ctreeview 创建了一个tree代码如下

int treeview::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;
    VERIFY (m_Image.Create (16, 15, TRUE, 0, 1));
    CBitmap bm;
    VERIFY (bm.LoadBitmap (IDR_MAINFRAME));
    VERIFY (-1 != m_Image.Add (&bm, RGB (192, 192, 192)));
    // create CTreeCtrl
    VERIFY (m_Tree.Create (WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_SHOWSELALWAYS, CRect (0, 0, 0, 0), this, IDI_ICON1));
    m_Tree.MoveWindow(20,20,1000,1000);
    m_Tree.SetImageList (&m_Image, TVSIL_NORMAL);
    TV_INSERTSTRUCT tvis;
    TV_ITEM tvi;
    
    char *szLabels[] = {    "New",
                            "Open",
                            "Save",
                            "Cut",
                            "Copy",
                            "Paste",
                            "Print",
                            "Help",
                            "About"    };
    // insert items into CTreeCtrl
    for (int iCntr = 0; iCntr < 9; iCntr++)
    {
        tvi.mask            = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
        tvi.pszText            = szLabels[iCntr];
        tvi.iImage            = iCntr;
        tvi.iSelectedImage    = iCntr;
 
        tvis.hParent        = NULL;
        tvis.hInsertAfter    = TVI_LAST;
        tvis.item            = tvi;
        VERIFY (m_Tree.InsertItem (&tvis));
    }
return 0;
}

你可以 用 你 原来 的 Tree 初始化程序, 但 一定 注意 这个 Tree 是 CTreeCtrl &refCtrl=GetTreeCtrl(); 得来

扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1