1、生成树
m_lstTree.DeleteAllItems();
HTREEITEM hRoot,hCur;//树控制项目句柄
HTREEITEM aItem;
TV_INSERTSTRUCT TCItem;//插入数据项数据结构
TCItem.hParent=TVI_ROOT;//增加根项
TCItem.hInsertAfter=TVI_LAST;//在最后项之后
TCItem.item.mask=TVIF_TEXT|TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;//设屏蔽
TCItem.item.pszText="场景";
TCItem.item.lParam=0;//序号
hRoot=m_lstTree.InsertItem(&TCItem);//返回根项句柄
m_lstTree.SetCheck(hRoot,true);
TCItem.hParent=hRoot;
TCItem.item.pszText="建筑物";
hCur=m_lstTree.InsertItem(&TCItem);
m_lstTree.SetCheck(hCur,true);
TCItem.hParent=hCur;
TCItem.item.pszText=“建筑物1”;
aItem=m_lstTree.InsertItem(&TCItem);
m_lstTree.SetCheck(aItem,true);
TCItem.item.pszText=“建筑物2”;
aItem=m_lstTree.InsertItem(&TCItem);
m_lstTree.SetCheck(aItem,true);
m_lstTree.Expand(hCur,TVE_EXPAND);
m_lstTree.Expand(hRoot,TVE_EXPAND);//展开上一级树
2、节点的双击事件
添加NM_DBLCLK(双击)消息
void CTreeCtrlDlg::OnDblClkTree(NMHDR* pNMHDR, LRESULT* pResult)
{
m_hTreeItem = m_wndTree.GetSelectedItem();
//以GetItemText()函数为例:
CString S1 = m_wndTree.GetItemText(m_hTreeItem);
AfxMessageBox("You have selected "+S1);
*pResult = 0;
}
3、点击checkbox事件的响应处理
重载 NM_CLICK事件
void CLayerTree::OnNMClickTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: 在此添加控件通知处理程序代码
CPoint pt;
UINT flag;
HTREEITEM hCurrentItem,TItem;
GetCursorPos(&pt);
m_lstTree.ScreenToClient(&pt);
hCurrentItem = m_lstTree.HitTest(pt, &flag);
m_lstTree.Select(hCurrentItem,TVGN_CARET);
CString selModeName,selModeName1;
if (flag & TVHT_ONITEMSTATEICON ) //点击在CheckBox上
{
selModeName1=m_lstTree.GetItemText(hCurrentItem);
if (m_lstTree.GetCheck(hCurrentItem))
{
}
else
{
}}
if( flag & TVHT_ONITEMBUTTON ) // 点击在+号上
{
MessageBox("");
}
*pResult = 0;}
4、递归搜索子节点void CMutiTreeCtrl::TravelChild(HTREEITEM hItem, int nState) { HTREEITEM hChildItem,hBrotherItem; //查找子节点,没有就结束 hChildItem=GetChildItem(hItem); if(hChildItem!=NULL) { //设置子节点的状态与当前节点的状态一致 CTreeCtrl::SetItemState(hChildItem,INDEXTOSTATEIMAGEMASK(nState), TVIS_STATEIMAGEMASK ); //再递归处理子节点的子节点和兄弟节点 TravelChild(hChildItem, nState); //处理子节点的兄弟节点和其子节点 hBrotherItem=GetNextSiblingItem(hChildItem); while (hBrotherItem) { //设置子节点的兄弟节点状态与当前节点的状态一致 int nState1 = GetItemState( hBrotherItem, TVIS_STATEIMAGEMASK ) >> 12; if(nState1!=0) { CTreeCtrl::SetItemState( hBrotherItem, INDEXTOSTATEIMAGEMASK(nState),TVIS_STATEIMAGEMASK ); } //再递归处理子节点的兄弟节点的子节点和兄弟节点 TravelChild(hBrotherItem, nState); hBrotherItem=GetNextSiblingItem(hBrotherItem); } } }
5、递归搜索兄、父节点void CMutiTreeCtrl::TravelSiblingAndParent(HTREEITEM hItem, int nState) { HTREEITEM hNextSiblingItem,hPrevSiblingItem,hParentItem; //查找父节点,没有就结束 hParentItem=GetParentItem(hItem); if(hParentItem!=NULL) { int nState1=nState;//设初始值,防止没有兄弟节点时出错 //查找当前节点下面的兄弟节点的状态 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) { //查找当前节点上面的兄弟节点的状态 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) { //如果状态一致,则父节点的状态与当前节点的状态一致 CTreeCtrl::SetItemState( hParentItem, INDEXTOSTATEIMAGEMASK(nState), TVIS_STATEIMAGEMASK ); } //再递归处理父节点的兄弟节点和其父节点 TravelSiblingAndParent(hParentItem,nState); } else { //状态不一致,则当前节点的父节点、父节点的父节点……状态均为第三态 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); } } } }