研究了好幾天,今天終於弄好了,下面是代碼
[dlg.cpp]
在初始函數OnInitDialog()中加
HICON hIcon[3];
HTREEITEM hRoot;
//加載三個圖標
hIcon[0] = theApp.LoadIconW(IDI_ICON1);
hIcon[1] = theApp.LoadIconW(IDI_ICON3);
hIcon[2] = theApp.LoadIconW(IDI_ICON2);web
m_imageList.Create(16, 16, ILC_COLOR16, 3, 3); for (int i=0;i<3;i++) { m_imageList.Add(hIcon[i]); } //綁定樹控件與ImaageList m_webTree.SetImageList(&m_imageList, TVSIL_NORMAL); //初始化Ado環境 AfxOleInit(); hRoot = NULL; OpenConn(); //打開數據函數 LoadTreeCtrlData(hRoot,_T("0")); //遞歸加數據到樹控件中 CloseConn(); //關閉數據庫
//下面就是遞歸函數
void CMFCApp_CTreeCtrl_testDlg::LoadTreeCtrlData(HTREEITEM hItem,CString mid)
{
HTREEITEM temItem;
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
CString sql;
sql.Format(_T("select * from mTable where pID='%s'"), mid);sql
m_pRecordset->Open((_variant_t)sql, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); //AfxMessageBox(_T("123")); } catch (_com_error e) { CString str; str.Format(_T("鏈接數據表失敗:%s"), e.ErrorMessage()); AfxMessageBox(str); return; } _variant_t var; CString strTmp; while (!m_pRecordset->adoEOF) { var = m_pRecordset->GetCollect(_T("mName")); strTmp = (LPCTSTR)_bstr_t(var); temItem=m_webTree.InsertItem(strTmp, 1, 1, hItem, TVI_LAST); //把本身的mID存入到節點中,
//節點存放的是指針地址的數據,查了好幾天百度才弄清.數據庫
var = m_pRecordset->GetCollect(_T("mID")); strTmp = (LPCTSTR)_bstr_t(var); CString* pStr = new CString; *pStr = strTmp; m_webTree.SetItemData(temItem,(DWORD)pStr); //遞歸加載子節點 LoadTreeCtrlData(temItem,strTmp); m_pRecordset->MoveNext(); }
}函數
//取出數據ui
CString strText;
HTREEITEM hItem = m_webTree.GetSelectedItem();
//strText = m_webTree.GetItemText(hItem);
CString str = (CString)m_webTree.GetItemData(hItem);
if(str!=NULL)
strText.Format(_T("%s"), str->GetString());
SetDlgItemText(IDC_ITEM_SET_EDIT,strText);指針
儘管磕磕絆絆的好幾天,今天總算弄出效果了.code