科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件用VC实现按数据库记录构建树控件

用VC实现按数据库记录构建树控件

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

将树中的每一个项目作为数据库中的一条记录(ACCESS2000),将程序启动时,对数据库进行读操作。

作者:XuFeng Yuan 来源:vckbase 2007年10月19日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
二.Recordset的创建:

  1.在CTreeDataDlg.h中声明变量:

//------------------------------------------
private:
HRESULT hRes;
_RecordsetPtr m_TreeRecordset; //用于创建一个查询记录集
//------------------------------------------
public:
CImageList m_TreeBootImage; //Tree的图标

  2. (1).在对话框窗口中添加一个TreeCtrl控件,一个ComboExe控件; TreeCtrl的风格设置如下图;


  (2).导入一个BMP文件,做为Tree的项目图标(TreeBoot.bmp),将其ID设置为IDB_TreeBootImage;

  (3).在向导中,为三个控件添加连接对象.

  3.在CTreeDataDlg中右击,选择添加一个成员函数

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);
}

  4.添加一个COM变量到CString变量的转换函数:

//-----------------实现了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;
}

  5.同样的方法添加另外一个成员函数

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);
}
}

  6.添加一个求当前项子项串的成员函数

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;
}

  7.处理TreeCtrl控件的点击(OnClick)和改变选择项(SelchangedTree)事件:

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;
}

  三.在BOOL CTreeDataDlg::OnInitDialog()中添加以下代码: TreeAddTree();

  本文效果图:


  总结:

  这个程序主要是在数据库中进行操作,主干是两个递归成员函数;对于递归,让你自己来理解吧!

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章