單文檔建的樹,想實現雙擊展開/閉合,且圖標要變化
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