鼠标点击某个项,怎样获取它是属于第几级目录?以及它这级目录中排第几?
看了下资料,似乎只能获得选中项的文本,而不能获取它的位置信息?

解决方案 »

  1.   


    void CXXXDlg::TraveTree(HTREEITEM hRoot, const HTREEITEM& hItem, int &nCount)
    {
    if(!hItem || !hRoot || hRoot == hItem)
    {
    return ;
    }
    HTREEITEM hChildItem;
    HTREEITEM hNextItem;

    nCount ++;
    hChildItem = m_tree.GetChildItem(hRoot);
    TraveTree(hChildItem, hItem, nCount); hNextItem = m_tree.GetNextSiblingItem(hRoot);
    TraveTree(hNextItem, hItem, nCount);
    }// 调用
    HTREEITEM hItem = m_tree.GetSelectedItem();
    if(hItem)
    {
    int nDir = 1;
    int nCount = 1; HTREEITEM hNextItem = hItem;
    while(NULL != (hNextItem = m_tree.GetParentItem(hNextItem)))
    {
    nDir++;
    } TraveTree(m_tree.GetRootItem(), hItem, nCount);
    CString strText(_T(""));
    strText.Format(_T("目录级别:%d, 位置索引:%d"), nDir, nCount);
    AfxMessageBox(strText);
    }
      

  2.   

    我觉得要知道索引值就一开始就设置好,SetItemData()
    这样的话,取的时候,也不需要递归了,GetItemData()
      

  3.   

    我最近也用了个树形控件,也遇到类似的问题。
    我最后没用获取位置的方式解决。方法有点傻,用的是判断选择ITEM的文字进行定位的void CPicTreeView::OnTvnSelchanged(NMHDR *pNMHDR, LRESULT *pResult)
    {
    LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
    // TODO: Add your control notification handler code here
    CTreeCtrl& CtlTree=(CTreeCtrl&)GetTreeCtrl(); //获得选项句柄
    HTREEITEM select_htem=CtlTree.GetSelectedItem();
    CString select_str=CtlTree.GetItemText(select_htem);
    //获得父节点句柄
    HTREEITEM parent_htem=CtlTree.GetParentItem(select_htem);
    CString p_str=CtlTree.GetItemText(parent_htem); if (p_str==_T(""))
    return; if (p_str==_T("影像匹配"))
    {
    if (select_str==_T("左片影像")&&!bLeft) //左片
    {
    bLeft=!bLeft;
    CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();
    CRect rect;
    //获得该视图的大小
    (((CMainFrame*)AfxGetMainWnd())-> m_wndSplitter.GetPane(0,1))->GetClientRect(rect);
    pMainFrame->m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(CLeftView),CSize(rect.Width(),rect.Height()),1,1,0);
    return;
    }
    if (select_str==_T("右片影像")&&!bRight) //左片
    {
    bRight=!bRight;
    CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();
    CRect rect;
    //获得该视图的大小
    (((CMainFrame*)AfxGetMainWnd())-> m_wndSplitter.GetPane(0,2))->GetClientRect(rect);
    pMainFrame->m_wndSplitter.ReplaceView(0,2,RUNTIME_CLASS(CRightView),CSize(rect.Width(),rect.Height()),1,2,0);
    return; }
    }
    if (p_str==_T("左片影像"))
    {
    if (select_str==_T("特征点列表")&&bLeft)
    {
    bLeft=!bLeft;
    CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();
    CRect rect;
    //获得该视图的大小
    (((CMainFrame*)AfxGetMainWnd())-> m_wndSplitter.GetPane(0,1))->GetClientRect(rect);
    pMainFrame->m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(CLeftList),CSize(rect.Width(),rect.Height()),0,0,0);
    return;
    }
    CPicMatchDoc *pDoc=((CPicTreeView*)(((CMainFrame*)AfxGetMainWnd())-> m_wndSplitter.GetPane(0,0)))->GetDocument();
    for(int li=0;li<pDoc->nImgL;li++)
    {
    CString strl;
    strl.Format(_T("第 %d 层"),li+1);
    if (select_str==strl&&!bLeft) //左片
    {
    bLeft=!bLeft;
    CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();
    CRect rect;
    //获得该视图的大小
    (((CMainFrame*)AfxGetMainWnd())-> m_wndSplitter.GetPane(0,1))->GetClientRect(rect);
    pMainFrame->m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(CLeftView),CSize(rect.Width(),rect.Height()),1,1,pDoc->nImgL-1-li);
    return;
    }
    CLeftView * pLView= (CLeftView*)(((CMainFrame*)AfxGetMainWnd())-> m_wndSplitter.GetPane(0,1));
    if (select_str==strl&&bLeft&&pDoc->nImgL-1-li!=pLView->nLayer)
    {
    pLView->nLayer=pDoc->nImgL-1-li;
    pLView->Invalidate(NULL);
    return;
    } }

    }
    if (p_str==_T("右片影像"))
    {
    if (select_str==_T("特征点列表")&&bRight)
    {
    bRight=!bRight;
    CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();
    CRect rect;
    //获得该视图的大小
    (((CMainFrame*)AfxGetMainWnd())-> m_wndSplitter.GetPane(0,2))->GetClientRect(rect);
    pMainFrame->m_wndSplitter.ReplaceView(0,2,RUNTIME_CLASS(CRightList),CSize(rect.Width(),rect.Height()),0,0,0);
    return;
    }
    CPicMatchDoc *pDoc=((CPicTreeView*)(((CMainFrame*)AfxGetMainWnd())-> m_wndSplitter.GetPane(0,0)))->GetDocument();
    for(int li=0;li<pDoc->nImgR;li++)
    {
    CString strr;
    strr.Format(_T("第 %d 层"),li+1);
    if (select_str==strr&&!bRight) //左片
    {
    bRight=!bRight;
    CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();
    CRect rect;
    //获得该视图的大小
    (((CMainFrame*)AfxGetMainWnd())-> m_wndSplitter.GetPane(0,2))->GetClientRect(rect);
    pMainFrame->m_wndSplitter.ReplaceView(0,2,RUNTIME_CLASS(CRightView),CSize(rect.Width(),rect.Height()),1,2,pDoc->nImgR-1-li);
    return;
    } CRightView * pRView= (CRightView*)(((CMainFrame*)AfxGetMainWnd())-> m_wndSplitter.GetPane(0,2));
    if (select_str==strr&&bLeft&&pDoc->nImgR-1-li!=pRView->nLayer)
    {
    pRView->nLayer=pDoc->nImgR-1-li;
    pRView->Invalidate(NULL);
    return;
    } }
    } *pResult = 0;
    }
      

  4.   

    点击当前项目的问题解决了,用双击就可以了。因为单击的时候已经选中了,所以双击GetSelectedItem()返回的句柄就是当前项目了。
    开始用的单击触发,GetSelectedItem()返回的是上一个的句柄。现在就剩最后一个问题了,你的这个索引值是怎么计算的?我希望得到的nCount是该目录下的重新一次排列的,每一级目录的子项都是从头排的,1,2,3,4....这样。
    我先自己试一下吧,主要对递归的理解不够
      

  5.   

    你这个方法我也想过。这个算法要求所有项目的文字都不能相同。而且没办法区别它所在第几级目录。
    不过有一种方法,就是不同的目录节点后面加上不同的符号区别。比如根目录节点文字后面加>>>,一级目录加>>,二级目录加>,子节点就什么都不加。处理的时候判断字符串来区别就行了。
    不过还是visualeleven的方法科学。
      

  6.   


    可以确定第几级,根据父节点的ITEMTEXT判断。
    一般同一级中需要相同文字的很少。上下级遇到相同文字是根据父节点的文字不同进行区分。当然,我这是第一次用TreeCtrl,所以很多函数不熟,考虑不是很周全。
      

  7.   

    你所说的索引值是指同级目录下的索引顺序吗?
    就比如说根节点下有A、B、C三个节点,而A下又有A1,A2两个节点,当前选中B节点?要求得出的索引值为2??是这个意思???
      

  8.   

    这么说吧,以我上面的那个例子来说
    比如说根节点下有A、B、C三个节点,而A下又有A1,A2两个节点,当前选中B节点?要求得出的索引值为2
    ----------------------------------------------------------------------------------
    你要得到B的索引值,
    你遍历的话,你要先得到该选择节点的父节点,B的父节点是根节点,那么你就要从根节点开始遍历,依次找到B,结束就得到了B的的索引值。如果你要得到A1的索引,那么同上,你要先得到A1的父节点A,然后从A节点开始遍历,找到A1,结束,得到A1
    的索引值。这样应该很清楚了吧
      

  9.   

    你要得到同级下的索引的话,就不用遍历子节点了,
    还是上面的例子,你要得到C节点的索引,从根节点开始,遍历得到A,就不用遍历A的子节点了,即遍历兄弟节点即可