分类: C/C++
2008-08-05 13:55:19
//----------------提取当前所选择项的子项文本所组成的字符串------------------------ CString CTreeDataDlg::ReturnTreeChilds(HTREEITEM hP) { CString text=""; //取出当前的Item值 text=m_ctrlTree.GetItemText(hP); //取子项的Item值 HTREEITEM hC; hC=m_ctrlTree.GetChildItem(hP); while(true) { //-----------判断是否有子项,如果有,则递归调用----------- if (m_ctrlTree.GetChildItem(hC)!=NULL) { text =("," ReturnTreeChilds(hC));} else {text =("," m_ctrlTree.GetItemText(hC));} //----------------判断是否有兄弟项,如果有,则将它的内容添加进去--- if (m_ctrlTree.GetNextSiblingItem(hC)==NULL) break; HTREEITEM hBC; hBC=m_ctrlTree.GetNextSiblingItem(hC); hC=hBC; } return text; }注:
二、 将void CTreeDataDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult)代码修改如下:
void CTreeDataDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; // TODO: Add your control notification handler code here //-------------------------------------------------------- hTreeCurrent=m_ctrlTree.GetSelectedItem (); hTreeParent=m_ctrlTree.GetParentItem(m_ctrlTree.GetSelectedItem ()); //-------------------树型控件的图标更改--------- m_ctrlTree.SetItemImage(hTreeCurrent,1,true ); //---------------------------------------------- TreeParent=m_ctrlTree.GetItemText (hTreeParent); hTreeCurrent=m_ctrlTree.GetSelectedItem (); //---------------------处理ListTree中的相应显示内容-------------- //-----------------获取当前的Item和子Items----------------------- if (m_ctrlTree.GetChildItem(hTreeCurrent)!=NULL) {//-------------如果,有子项则进行递归操作,返回自己和子项的值--------- m_strEdit=ReturnTreeChilds(hTreeCurrent); } else { m_strEdit=m_ctrlTree.GetItemText(hTreeCurrent); } UpdateData(false);//子项内容显示到Edit控件中 //--------------------------------------------------- *pResult = 0; }这样,就可以实现点击树节点时,程序自动去搜集当前节点与子节点的值集。 下载本文示例代码