四、檢測鍵盤按空格鍵的事件,更改對應(yīng)的節(jié)點(diǎn)狀態(tài)并遍歷樹的其他節(jié)點(diǎn)。 void CMutiTreeCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
//處理空格鍵
if(nChar==0x20)
{
HTREEITEM hItem =GetSelectedItem();
UINT nState = GetItemState( hItem, TVIS_STATEIMAGEMASK ) >> 12;
if(nState!=0)
{
nState=(nState==3)?1:3;
SetItemState( hItem, INDEXTOSTATEIMAGEMASK(nState),
TVIS_STATEIMAGEMASK );
}
}
else CTreeCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}
五、樹的遍歷用遞歸的方法搜索當(dāng)前節(jié)點(diǎn)的父、兄、子節(jié)點(diǎn)
?、龠f歸搜索子節(jié)點(diǎn)void CMutiTreeCtrl::TravelChild(HTREEITEM hItem, int nState)
{
HTREEITEM hChildItem,hBrotherItem;
//查找子節(jié)點(diǎn),沒有就結(jié)束
hChildItem=GetChildItem(hItem);
if(hChildItem!=NULL)
{
//設(shè)置子節(jié)點(diǎn)的狀態(tài)與當(dāng)前節(jié)點(diǎn)的狀態(tài)一致
CTreeCtrl::SetItemState(hChildItem,INDEXTOSTATEIMAGEMASK(nState),
TVIS_STATEIMAGEMASK );
//再遞歸處理子節(jié)點(diǎn)的子節(jié)點(diǎn)和兄弟節(jié)點(diǎn)
TravelChild(hChildItem, nState);
//處理子節(jié)點(diǎn)的兄弟節(jié)點(diǎn)和其子節(jié)點(diǎn)
hBrotherItem=GetNextSiblingItem(hChildItem);
while (hBrotherItem)
{
//設(shè)置子節(jié)點(diǎn)的兄弟節(jié)點(diǎn)狀態(tài)與當(dāng)前節(jié)點(diǎn)的狀態(tài)一致
int nState1 = GetItemState( hBrotherItem, TVIS_STATEIMAGEMASK ) >> 12;
if(nState1!=0)
{
CTreeCtrl::SetItemState( hBrotherItem,
INDEXTOSTATEIMAGEMASK(nState),TVIS_STATEIMAGEMASK );
}
//再遞歸處理子節(jié)點(diǎn)的兄弟節(jié)點(diǎn)的子節(jié)點(diǎn)和兄弟節(jié)點(diǎn)
TravelChild(hBrotherItem, nState);
hBrotherItem=GetNextSiblingItem(hBrotherItem);
}
}
}
?、谶f歸搜索兄、父節(jié)點(diǎn) void CMutiTreeCtrl::TravelSiblingAndParent(HTREEITEM hItem, int nState)
{
HTREEITEM hNextSiblingItem,hPrevSiblingItem,hParentItem;
//查找父節(jié)點(diǎn),沒有就結(jié)束
hParentItem=GetParentItem(hItem);
if(hParentItem!=NULL)
{
int nState1=nState;//設(shè)初始值,防止沒有兄弟節(jié)點(diǎn)時(shí)出錯(cuò)
//查找當(dāng)前節(jié)點(diǎn)下面的兄弟節(jié)點(diǎn)的狀態(tài)
hNextSiblingItem=GetNextSiblingItem(hItem);
while(hNextSiblingItem!=NULL)
{
nState1 = GetItemState( hNextSiblingItem, TVIS_STATEIMAGEMASK ) >> 12;
if(nState1!=nState && nState1!=0) break;
else hNextSiblingItem=GetNextSiblingItem(hNextSiblingItem);
}
if(nState1==nState)
{
//查找當(dāng)前節(jié)點(diǎn)上面的兄弟節(jié)點(diǎn)的狀態(tài)
hPrevSiblingItem=GetPrevSiblingItem(hItem);
while(hPrevSiblingItem!=NULL)
{
nState1 = GetItemState(hPrevSiblingItem,TVIS_STATEIMAGEMASK)>> 12;
if(nState1!=nState && nState1!=0) break;
else hPrevSiblingItem=GetPrevSiblingItem(hPrevSiblingItem);
}
}
if(nState1==nState nState1==0)
{
nState1 = GetItemState( hParentItem, TVIS_STATEIMAGEMASK ) >> 12;
if(nState1!=0)
{
//如果狀態(tài)一致,則父節(jié)點(diǎn)的狀態(tài)與當(dāng)前節(jié)點(diǎn)的狀態(tài)一致
CTreeCtrl::SetItemState( hParentItem,
INDEXTOSTATEIMAGEMASK(nState), TVIS_STATEIMAGEMASK );
}
//再遞歸處理父節(jié)點(diǎn)的兄弟節(jié)點(diǎn)和其父節(jié)點(diǎn)
TravelSiblingAndParent(hParentItem,nState);
}
else
{
//狀態(tài)不一致,則當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)、父節(jié)點(diǎn)的父節(jié)點(diǎn)……狀態(tài)均為第三態(tài)
hParentItem=GetParentItem(hItem);
while(hParentItem!=NULL)
{
nState1 = GetItemState( hParentItem, TVIS_STATEIMAGEMASK ) >> 12;
if(nState1!=0)
{
CTreeCtrl::SetItemState( hParentItem,
INDEXTOSTATEIMAGEMASK(2), TVIS_STATEIMAGEMASK );
}
hParentItem=GetParentItem(hParentItem);
}
}
}
}
好了,一切就是這么簡單,如果你還不清楚的話,那就打開工程看看吧,如你有什么問題也不要忘記來信告訴我哦!最后祝大家學(xué)習(xí)愉快,多多交流,多多進(jìn)步,一切順利!