⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clienttreedlgview.cpp

📁 用数据库控制树型控件现实内容的例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ClientTreeDlgView.cpp : implementation file
//

#include "stdafx.h"
#include "Kill.h"
#include "ClientTreeDlgView.h"
#include "ItemDeleteDlg.h"
#include "ItemEditDlg.h"
#include "ItemNewDlg.h"
#include "MainFrm.h"
#include "InstallKillDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CClientTreeDlgView

IMPLEMENT_DYNCREATE(CClientTreeDlgView, CFormView)

CClientTreeDlgView::CClientTreeDlgView()
	: CFormView(CClientTreeDlgView::IDD)
{
	//{{AFX_DATA_INIT(CClientTreeDlgView)
	//}}AFX_DATA_INIT
	CoInitialize(NULL);
//	AfxOleInit();
//    m_db.CreateInstance(__uuidof(Connection));
    HRESULT hRes;
	try
	{
        hRes=m_db.CreateInstance(_T("ADODB.Connection"));
		m_db->ConnectionTimeout = 8;
		//连接ACCESS2000
		hRes=m_db->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DataBase\\KillData.mdb",
            _T(""),_T(""),adModeUnknown);
	}
	catch(_com_error e)///捕捉异常
	{
		 CString errormessage;
		 errormessage.Format(_T("连接KillData.mdb数据库失败!\r\n错误信息:%s"),e.ErrorMessage());
		 AfxMessageBox(errormessage);///显示错误信息
		 exit(0);
	} 

}

CClientTreeDlgView::~CClientTreeDlgView()
{
	m_db->Close();
	CoUninitialize();
}

void CClientTreeDlgView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClientTreeDlgView)
	DDX_Control(pDX, IDC_CLIENT_TREE, m_ClientTree);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CClientTreeDlgView, CFormView)
	//{{AFX_MSG_MAP(CClientTreeDlgView)
	ON_WM_SIZE()
	ON_NOTIFY(TVN_SELCHANGED, IDC_CLIENT_TREE, OnSelchangedClientTree)
	ON_COMMAND(IDR_POPUP_NEW, OnPopupNew)
	ON_COMMAND(IDR_POPUP_INSTALL, OnPopupInstall)
	ON_COMMAND(IDR_POPUP_EDIT, OnPopupEdit)
	ON_COMMAND(IDR_POPUP_DELETE, OnPopupDelete)
	ON_COMMAND(IDR_POPUP_UNINSTALL, OnPopupUninstall)
	ON_NOTIFY(NM_DBLCLK, IDC_CLIENT_TREE, OnDblclkClientTree)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientTreeDlgView diagnostics

#ifdef _DEBUG
void CClientTreeDlgView::AssertValid() const
{
	CFormView::AssertValid();
}

void CClientTreeDlgView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CClientTreeDlgView message handlers

void CClientTreeDlgView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if(m_ClientTree.m_hWnd)
		m_ClientTree.SetWindowPos(this,0,0,cx,cy,SWP_SHOWWINDOW|SWP_NOZORDER );
}

void CClientTreeDlgView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
//	m_ClientTree.SetBkColor(RGB(220,220,220));
	// TODO: Add your specialized code here and/or call the base class
//-----------------创建图象标签----------------------------
//	m_TreeBootImage.Create ( IDB_TreeBootImage,20,1,ILC_COLOR8);
	m_TreeBootImage.Create ( IDB_BITMAP_TREE,18,1,ILC_COLOR8);
	m_ClientTree.SetImageList ( &m_TreeBootImage,TVSIL_NORMAL );
/*
	CFont cf;

	VERIFY(cf.CreateFont(
   12,                        // nHeight
   0,                         // nWidth
   0,                         // nEscapement
   0,                         // nOrientation
   FW_NORMAL,                 // nWeight
   FALSE,                     // bItalic
   FALSE,                     // bUnderline
   0,                         // cStrikeOut
   GB2312_CHARSET,              // nCharSet
   OUT_DEFAULT_PRECIS,        // nOutPrecision
   CLIP_DEFAULT_PRECIS,       // nClipPrecision
   DEFAULT_QUALITY,           // nQuality
   DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
   "宋体"));                 // lpszFacename  Arial
	m_ClientTree.SetFont(&cf,TRUE);
*/
	AddTree();
}

void CClientTreeDlgView::AddTree()
{
//--------------Tree控件操作变量------------------------
	TVINSERTSTRUCT tvInsert;
	HTREEITEM hParent;

//------------------------------------------------
	tvInsert.hParent = NULL;
	tvInsert.hInsertAfter = NULL;
	tvInsert.item.mask = TVIF_TEXT;
	
//--------添加根目录----------------------------------------
	tvInsert.item.pszText = _T("组织");
	hParent = m_ClientTree.InsertItem(&tvInsert);
	m_ClientTree.SetItemData(hParent,1);
	m_ClientTree.SetItemImage(hParent,1,true );
/**/
//---------------添加子目录-------------------------------
	AddSubTree(1,"1",hParent);
//---------------------展开Tree目录------------------
	m_ClientTree.Expand(hParent,TVE_EXPAND);
}

void CClientTreeDlgView::AddSubTree(int ParTree, CString strChildTree, HTREEITEM hPartItem)
{
	if (strChildTree=="0")
		return  ;
		//----------------使用到的变量进行定义----------
	_RecordsetPtr m_pTreeRecordset;    //用于创建一个查询记录集
	_variant_t vChild;
		//--------------Tree控件操作变量------------------------
	HTREEITEM hCurrent;
	//----------------------------------------------
	CString strSQL,strCurItemstr;
		//-----------------------------------------------
	strSQL.Format("SELECT * FROM ComputerGroups where ParentComputerGroupid = %d",ParTree );

	_variant_t varSQLtcp(strSQL);
	try
	{
		HRESULT hTRes;
	    hTRes = m_pTreeRecordset.CreateInstance(_T("ADODB.Recordset"));
		if (SUCCEEDED(hTRes))
		{
            //----------------------------------------------------
	        hTRes = m_pTreeRecordset->Open(varSQLtcp,
		         _variant_t((IDispatch *)m_db,true),
				 adOpenStatic,adLockOptimistic,adCmdText);
//			    adOpenDynamic,adLockPessimistic,adCmdText);  //这个为什么不行,还要考虑
            if(SUCCEEDED(hTRes))
	        {
				TRACE(_T("连接成功!\n"));
			//------------------------------------------
				if (!(m_pTreeRecordset->adoEOF))
				{
//					m_ClientTree.SetItemImage(hPartItem,1,true );
					m_pTreeRecordset->MoveFirst();
				}
				if (m_pTreeRecordset->GetRecordCount()>0)
				{
					for(int i=0;i<m_pTreeRecordset->GetRecordCount(); i++)
//					while (!(m_pTreeRecordset->adoEOF))
					{
						hCurrent = m_ClientTree.InsertItem((LPCTSTR)(_bstr_t)\
							(m_pTreeRecordset->GetCollect("ComputerGroupName")), hPartItem, NULL);
						m_ClientTree.SetItemData(hCurrent, m_pTreeRecordset->GetCollect("ComputerGroupId").intVal );

						strCurItemstr=m_pTreeRecordset->GetCollect("ComputerGroupName").bstrVal;
						if(strCurItemstr.Compare("组织")==0)
							m_ClientTree.SetItemImage(hCurrent,1,true );


						AddSubTree(m_pTreeRecordset->GetCollect("ComputerGroupId").intVal,
								m_pTreeRecordset->GetCollect("ComputerGroupName").bstrVal,
								hCurrent);
						
						if (!(m_pTreeRecordset->adoEOF))
							m_pTreeRecordset->MoveNext();
					}		
				}
		//---------------------------------------
			}
		}
	}
	catch(_com_error e)///捕捉异常
	{
		 CString errormessage;
		 MessageBox("创建City记录集失败!",strChildTree);
	}
}



void CClientTreeDlgView::OnSelchangedClientTree(NMHDR* pNMHDR, LRESULT* pResult) 
{

	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	CMainFrame * pMain=(CMainFrame *)AfxGetMainWnd();
	HTREEITEM hTreeCurrent;//当前和父级项句柄
	hTreeCurrent=m_ClientTree.GetSelectedItem ();
	int GroupID=m_ClientTree.GetItemData(hTreeCurrent);
	pMain->m_ClientInfoview->ShowClient(GroupID);
	*pResult = 0;
}

void CClientTreeDlgView::OnPopupNew() 
{
	// TODO: Add your command handler code here
	HTREEITEM hTreeCurrent;//当前项句柄
	hTreeCurrent=m_ClientTree.GetSelectedItem ();
	m_ClientTree.Expand(hTreeCurrent,TVE_EXPAND);

	CItemNewDlg dlg;
	if(dlg.DoModal()==IDOK)
	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -