qq_28254507
ProgJ
2016-02-16 11:11

急!MFC CTreeCtrl双击消息函数的问题

20
  • ctreectrl
  • c++
  • mfc

单文档建的树,想实现双击展开/闭合,且图标要变化

 void TreeView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
    HTREEITEM hItem = ptheTree->HitTest(point, &nFlags); //获得鼠标点击相关项
    CString s = ptheTree->GetItemText(hItem);

    if (hItem != NULL && !JudgeIsNotebook(hItem) && s !="回收站")
    {

        if (isExpand == false)           //还没展开,将其展开
        {
            changeRootBitmap(point, nFlags);    //切换图标
            ptheTree->SetItemState(hItem, 0, LVIS_SELECTED | LVIS_FOCUSED); //取消根项高亮
            if (ptheTree->GetChildItem(hItem))
           {
              HTREEITEM hChildItem = ptheTree->GetChildItem(hItem);
              ptheTree->SetItemState(hChildItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);              //将子项高亮
            }
         }
        else                              //已经展开,将其闭合
        {
            changeRootBitmap(point, nFlags);    //切换图标
            ptheTree->SetItemState(hItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);      //将根项高亮
         }
        isExpand = !isExpand;            //更改状态标识
    }
    //HTREEITEM item = ptheTree->GetSelectedItem();
    //ptheTree->SetItemState(item, 0, TVIS_SELECTED);

    CTreeView::OnLButtonDblClk(nFlags, point);
}

在根项未展开时,双击后一切正常,根项展开了,图标1也切换为了图标2,但再次双击时(想要闭合根项)就不行了,图标2也切换不回图标1了。
以下是用到的切换图标的函数:

 void TreeView::changeRootBitmap(CPoint point, UINT nFlags)
{
    HTREEITEM hItem = ptheTree->HitTest(point, &nFlags); 
    if (hItem != NULL && !JudgeIsNotebook(hItem) && isExpand == false)
    {
        ptheTree->SetItemImage(hItem, 1, 2);
    }
    else
        ptheTree->SetItemImage(hItem, 2, 1);
    /*CMainFrame *pMain = (CMainFrame *)AfxGetApp()->m_pMainWnd;
    CDocument * pCurrentDoc = (CDocument *)pMain->GetActiveDocument();
    pCurrentDoc->UpdateAllViews(NULL);*/

}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答

为你推荐