程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> c++-急!MFC CTreeCtrl雙擊消息函數的問題

c++-急!MFC CTreeCtrl雙擊消息函數的問題

編輯:編程解疑
急!MFC CTreeCtrl雙擊消息函數的問題

單文檔建的樹,想實現雙擊展開/閉合,且圖標要變化

 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);*/

}

最佳回答:


測試了你的程序,問題比較多,你應該用HitTest先得到當前操作的Item,對它處理,而不是對所有的Item處理。
http://blog.csdn.net/goingup/article/details/2932767

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved