扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:XuFeng Yuan 来源:vckbase 2007年10月19日
关键字:
//------------------------------------------ private: HRESULT hRes; _RecordsetPtr m_TreeRecordset; //用于创建一个查询记录集 //------------------------------------------ public: CImageList m_TreeBootImage; //Tree的图标 |
TreeAddTree(bool Ta): void CBusinessView::TreeAddTree(bool Ta) { //--------------Tree控件操作变量------------------------ TVINSERTSTRUCT tvInsert; HTREEITEM hParent; //------------------------------------------------ tvInsert.hParent = NULL; tvInsert.hInsertAfter = NULL; tvInsert.item.mask = TVIF_TEXT; //-----------------创建图象标签---------------------------- m_TreeBootImage.Create ( IDB_TreeBootImage,20,1,ILC_COLOR8); m_ctrlTree.SetImageList ( &m_TreeBootImage,TVSIL_NORMAL ); m_ctrlTree.SetTextColor (RGB(7,145,13)); //--------添加根目录---------------------------------------- tvInsert.item.pszText = _T("中国"); hParent = m_ctrlTree.InsertItem(&tvInsert); //---------------添加子目录------------------------------- TreeAddSubTree("中国","1",hParent); //---------------------展开Tree目录------------------ m_ctrlTree.Expand(hParent,TVE_EXPAND); } |
//-----------------实现了VARIANT类型的值转换成CString类型-------------- CString CBusinessView::VariantToCString(VARIANT var) { CString strValue; _variant_t var_t; _bstr_t bst_t; time_t cur_time; CTime time_value; COleCurrency var_currency; switch(var.vt) { case VT_EMPTY:strValue=_T("");break; case VT_UI1:strValue.Format ("%d",var.bVal);break; case VT_I2:strValue.Format ("%d",var.iVal );break; case VT_I4:strValue.Format ("%d",var.lVal);break; case VT_R4:strValue.Format ("%f",var.fltVal);break; case VT_R8:strValue.Format ("%f",var.dblVal);break; case VT_CY: var_currency=var; strValue=var_currency.Format(0); break; case VT_BSTR: var_t=var; bst_t=var_t; strValue.Format ("%s",(const char*)bst_t); break; case VT_NULL: strValue=_T(""); break; case VT_DATE: cur_time=var.date; time_value=cur_time; strValue=time_value.Format("%A,%B%d,%Y"); break; case VT_BOOL: strValue.Format ("%d",var.boolVal ); break; default: strValue=_T(""); break; } return strValue; } |
TreeAddSubTree(CString ParTree,CString strChildTree,HTREEITEM hPartItem): 此成员函数是一个递归函数. if (strChildTree!="0") { //----------------使用到的变量进行定义---------- _RecordsetPtr m_pTreeRecordset; //用于创建一个查询记录集 _variant_t vChild; //--------------Tree控件操作变量------------------------ HTREEITEM hCurrent; //---------------------------------------------- CString strSQL,strCurItem; //----------------------------------------------- strSQL="SELECT * FROM TreeItem where ParentItem like ''%" ; strSQL=strSQL+ParTree+"%''"; try { HRESULT hTRes; hTRes = m_pTreeRecordset.CreateInstance(_T("ADODB.Recordset")); if (SUCCEEDED(hTRes)) { //---------------------------------------------------- hTRes = m_pTreeRecordset->Open((LPTSTR)strSQL.GetBuffer(130), _variant_t((IDispatch *)(((CBusinessApp*)AfxGetApp())->m_pTreeConnection),true), adOpenDynamic,adLockPessimistic,adCmdText); if(SUCCEEDED(hTRes)) { TRACE(_T("连接成功!\n")); //------------------------------------------ m_pTreeRecordset->MoveFirst(); if (!(m_pTreeRecordset->adoEOF)) { while(!m_pTreeRecordset->adoEOF) { hCurrent = m_ctrlTree.InsertItem((LPCTSTR)(_bstr_t)\ (m_pTreeRecordset->GetCollect("Name")), hPartItem, NULL); //---------------将内容添加到City的Combo控件中------------------ m_ctrlComboCity.AddString(VariantToCString(m_pTreeRecordset->GetCollect("Name"))); if (TreeSumRecordCount(VariantToCString\ (m_pTreeRecordset->GetCollect("Name")))>0) { TreeAddSubTree(VariantToCString(m_pTreeRecordset->GetCollect("Name")), (VariantToCString(m_pTreeRecordset->GetCollect("Name"))), hCurrent); } if (!(m_pTreeRecordset->adoEOF)) { m_pTreeRecordset->MoveNext(); } } } //--------------------------------------- } } } catch(_com_error e)///捕捉异常 { CString errormessage; MessageBox("创建City记录集失败!",ParTree+strChildTree); } } |
ReturnTreeChilds(CString strCurItem): 此成员函数也是递归函数. //----------------提取当前所选择项的子项文本所组成的字符串------------------------ CString CTreeDataDlg::ReturnTreeChilds(CString strCurItem) { CString strTreeChildren;//记录子项文本所组成的字符串 if (TreeSumRecordCount(strCurItem) > 0) { //--------------------进入递归运算--------------------- _RecordsetPtr m_pTreeRecordset; //用于创建一个查询记录集 _variant_t vCur; CString strSQL; //----------------------------------------------- strSQL="SELECT * FROM TreeItem where ParentItem like ''%" ; strSQL=strSQL+strCurItem+"%''"; try { HRESULT hTRes; hTRes = m_pTreeRecordset.CreateInstance(_T("ADODB.Recordset")); if (SUCCEEDED(hTRes)) { //---------------------------------------------------- hTRes = m_pTreeRecordset->Open((LPTSTR)strSQL.GetBuffer(130), _variant_t((IDispatch *)(((CTreeDataApp*)AfxGetApp())->m_pTreeConn),true), adOpenDynamic,adLockPessimistic,adCmdText); if(SUCCEEDED(hTRes)) { TRACE(_T("连接成功!\n")); //------------------------------------------ m_pTreeRecordset->MoveFirst(); vCur=(m_pTreeRecordset->GetCollect("Name")); if (TreeSumRecordCount(VariantToCString(vCur))>=0) { while(!m_pTreeRecordset->adoEOF) { vCur=(m_pTreeRecordset->GetCollect("Name")); strTreeChildren+=(",''"+VariantToCString(vCur)+"''"); if (TreeSumRecordCount(VariantToCString(vCur))!=0) { strTreeChildren+=ReturnTreeChilds(VariantToCString(vCur)); } if (!(m_pTreeRecordset->adoEOF)) { m_pTreeRecordset->MoveNext(); } } } //--------------------------------------- } } } catch(_com_error e)///捕捉异常 { CString errormessage; AfxMessageBox("创建ChildTree记录集失败!"+strCurItem); } } return strTreeChildren; } |
void CTreeDataDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; //-------------------------------------------------------- hTreeCurrent=m_ctrlTree.GetSelectedItem (); hTreeParent=m_ctrlTree.GetParentItem(m_ctrlTree.GetSelectedItem ()); //-------------------树型控件的图标更改--------- m_ctrlTree.SetItemImage(hTreeCurrent,1,true ); //---------------------------------------------- TreeCurrent="''"+m_ctrlTree.GetItemText(hTreeCurrent)+"''"; TreeParent=m_ctrlTree.GetItemText (hTreeParent); //---------------------处理ListTree中的相应显示内容-------------- //-------------提取树中当前项及其子项的内容------ hTreeCurrent=m_ctrlTree.GetSelectedItem (); m_strEdit=TreeCurrent+ReturnTreeChilds(m_ctrlTree.GetItemText(hTreeCurrent)); UpdateData(false);//子项内容显示到Edit控件中 //--------------------------------------------------- *pResult = 0; } void CTreeDataDlg::OnClickTree1(NMHDR* pNMHDR, LRESULT* pResult) { //-------------------树型控件的图标还原--------- m_ctrlTree.SetItemImage(hTreeCurrent,0,true ); //---------------------------------------------- *pResult = 0; } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者