触摸屏租赁
Touch screen rental
广告机租赁
Advertising rental
机器人租赁
Robot leasing
 当前位置:首页 > > Tree Control控件调用方法

一、修改stdafx.h

// stdafx.h : include file for standard system include files,

。。。。。。。。。。。。。。。。。。

#endif // _AFX_NO_AFXCMN_SUPPORT

//--------------------------------------------------------------------

#import “c:\program files\common files\system\ado\msado15.dll”  \

no_namespace  \

rename(“EOF”,”adoEOF”)

//--------------------------------------------------------------------

//{{AFX_INSERT_LOCATION}}

。。。。。。。。。。。。。。。。。。

// !defined(AFX_STDAFX_H__3B4A3506_D97F_472C_8B84_5842AC9CE4F7__INCLUDED_)

二、在类 CTreeDataApp中加入公共类的成员变量m_pTreeConn和私有类的成员变量TreeConnString,代码如下:

// TreeData.h : main header file for the TREEDATA application

。。。。。。。。。。。。。。。。。。。。。。。。

class CTreeDataApp : public CWinApp

{

public:

       CTreeDataApp();

       _ConnectionPtr m_pTreeConn;//连接创建

// Overrides

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

protected:

       CString TreeConnString;//连接字符串

};

。。。。。。。。。。。。。。。。。。。。。。。。。。

三、右键建立向导,增加成员变量。

CTreeCtrl       m_ctrlTree;

四、TreeData.cpp中加入数据库文件调用代码:

。。。。。。。。。。。。。。。。。。。。

CTreeDataApp::CTreeDataApp()

{

       // TODO: add construction code here,

       // Place all significant initialization in InitInstance

       //-------------------------------------------------------

    TreeConnString=_T("Provider=Microsoft.Jet.OLEDB.4.0;")

        _T("Data Source=DataBase\\City.mdb;");

       //-------------------------------------------------------

}

。。。。。。。。。。。。。。。。。。。。。

五、TreeData.cpp中的InitInstance()函数中加入连接数据库文件代码:

BOOL CTreeDataApp::InitInstance()

{

       AfxEnableControlContainer();

//-------COM初始化--------------------------------

       AfxOleInit();

       /******************连接通讯录数据库********************/

    HRESULT hRes;

       try

       {

        hRes=m_pTreeConn.CreateInstance(_T("ADODB.Connection"));

              m_pTreeConn->ConnectionTimeout = 8;

              //连接ACCESS2000

              hRes=m_pTreeConn->Open(_bstr_t((LPCTSTR) TreeConnString),

            _T(""),_T(""),adModeUnknown);

       }

       catch(_com_error e)///捕捉异常

       {

               CString errormessage;

               errormessage.Format(_T("连接City.mdb数据库失败!\r\n错误信息:%s"),e.ErrorMessage());

               AfxMessageBox(errormessage);///显示错误信息

         return FALSE;

       }

 

//------------------------------------------------

。。。。。。。。。。。。。。。。。。。。。。。。。。

六、在对话框类CTreeDataDlg中加入公共类的成员变量m_TreeBootImage和私有类的成员变量m_TreeRecordset

// TreeDataDlg.h : header file

。。。。。。。。。。。。。。。。。。。。。。。

//-------------------------------------------------      

private:

       CString ReturnTreeChilds(HTREEITEM hP);

       int TreeSumRecordCount(CString strFieldValue);

       CString VariantToCString(VARIANT var);

       void TreeAddTree();

       HRESULT hRes;

       _RecordsetPtr m_TreeRecordset;    //用于创建一个查询记录集

//-------------------------------------------------

public:

//--------------------------------------------------------

       CImageList m_TreeBootImage;//Tree的图标

 

。。。。。。。。。。。。。。。。。。。。。

七、导入位图资源并命名为IDIDB_TreeBootImage

八、在类CTreeDataDlg中加入公共类私有类private成员函数TreeAddTree()类型为void,并在其中加入代码。

void CTreeDataDlg::TreeAddTree()

{

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

}

九、在类CTreeDataDlg中加入公共类public成员函数TreeAddSubTree(CString ParTree, CString strChildTree, HTREEITEM hPartItem)类型为void

void CTreeDataDlg::TreeAddSubTree(CString ParTree, CString strChildTree, HTREEITEM hPartItem)

{

 

}

十、在类CTreeDataDlg中加入私有类private成员函数TreeSumRecordCount(CString strFieldValue) 类型为int

int CTreeDataDlg::TreeSumRecordCount(CString strFieldValue)

{

}

十一、       十一、在类CTreeDataDlg中加入私有类private成员函数VariantToCString(VARIANT var) 类型为CString

CString CTreeDataDlg::VariantToCString(VARIANT var)

{

十二、分别在九、十、十一中加入代码:

void CTreeDataDlg::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 *)(((CTreeDataApp*)AfxGetApp())->m_pTreeConn),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);

       //---------------将内容添加到CityCombo控件中------------------

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

              }

       }

}

。。。。。。。。。。。。。。。。。。。。

int CTreeDataDlg::TreeSumRecordCount(CString strFieldValue)

{

       int Sum=0;

       //----------------使用到的变量进行定义----------

                     _RecordsetPtr m_pRecordset;    //用于创建一个查询记录集

                     _variant_t vChild;

              //----------------------------------------------

              CString strSQL,strCurItem;

              //-----------------------------------------------

              strSQL="SELECT * FROM TreeItem where ParentItem like '%" ;

              strSQL=strSQL+strFieldValue+"%'";

              try

              {

                     HRESULT hTRes;

                  hTRes = m_pRecordset.CreateInstance(_T("ADODB.Recordset"));

                     if (SUCCEEDED(hTRes))

                     {

                   //----------------------------------------------------

                      hTRes = m_pRecordset->Open((LPTSTR)strSQL.GetBuffer(130),

                             _variant_t((IDispatch *)(((CTreeDataApp*)AfxGetApp())->m_pTreeConn),true),

                                adOpenDynamic,adLockPessimistic,adCmdText);

                   if(SUCCEEDED(hTRes))

                      {

                                   TRACE(_T("连接成功!\n"));

                                   //------------------------------------------

                                   if(!m_pRecordset->BOF )

                                   {

                                          m_pRecordset->MoveFirst ();

                                          while(!m_pRecordset->adoEOF)

                                          {

                                                 Sum+=1;

                                                 m_pRecordset->MoveNext ();

                                          }

                                   }

                                   //---------------------------------------

                            }

                     }

              }

              catch(_com_error e)///捕捉异常

              {

                      CString errormessage;

                      MessageBox("求符合条件的记录总数出错!",strFieldValue);

              }

       return Sum;

}

。。。。。。。。。。。。。。。。。。。。。

CString CTreeDataDlg::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;

}

十三、在类CTreeDataDlg的成员函数OnInitDialog()加入代码:

BOOL CTreeDataDlg::OnInitDialog()

{

。。。。。。。。。。。。。。。。。。。。。。。

       SetIcon(m_hIcon, TRUE);                  // Set big icon

       SetIcon(m_hIcon, FALSE);          // Set small icon

      

       // TODO: Add extra initialization here

//------------------------------------------------

              TreeAddTree();

//------------------------------------------------

       return TRUE;  // return TRUE  unless you set the focus to a control

}


工作室地址:重庆石桥铺电脑城B座 | 微信:z35544216 | 网址:www.35544216.com