cdbtreenode.cpp

来自「本人买的<<VC++项目开发实例>>源代码配套光盘.」· C++ 代码 · 共 228 行

CPP
228
字号
// Copyright (C) 1991 - 1999 Rational Software Corporation

#include "stdafx.h"
#include "CDBTreeNode.h"
#include "resource.h"
#include "CDBExpAlias.h"

//##ModelId=3CCC337003C1
CTreeCtrl * CDBTreeNode::m_pTreeCtrl = NULL;
//##ModelId=3C3735120194
IMPLEMENT_DYNAMIC(CDBTreeNode, CObject )
//##ModelId=3C3735120194
BOOL CDBTreeNode::PopupContextMenu(DBTREE_NODE_CONTEXT &context)
{
	// TODO: Add your specialized code here.


	//DBTREE_NODE_CONTEXT中要放入的有
	//m_pos //当前的位置
	//m_NodeType//当前的数据结点的类型。
	//m_pParentWnd//当前的父窗口

	m_pContextMenu = new CMenu;

	BuildContextMenu(context);
	CMenu *pPopup = m_pContextMenu->GetSubMenu(0);

	BOOL nCmd = pPopup->TrackPopupMenu(
				TPM_LEFTALIGN |TPM_RIGHTBUTTON
				/*
				Value Meaning 
				TPM_NONOTIFY If this flag is set, the function does not send notification messages when the user clicks on a menu item. 
				TPM_RETURNCMD If this flag is set, the function returns the menu item identifier of the user's selection in the return value. 
				*/
				| TPM_NONOTIFY | TPM_RETURNCMD,
				context.m_pos.x, context.m_pos.y, context.m_pParentWnd);
	
	m_pContextMenu->DestroyMenu();
	delete m_pContextMenu;
	m_pContextMenu = NULL;
	
	if(0 != nCmd)
		OnMenuItemClick(nCmd);
	return nCmd;
}

//##ModelId=3C37358B0238
void CDBTreeNode::BuildContextMenu(DBTREE_NODE_CONTEXT & context)
{
	// TODO: Add your specialized code here.
	//加载最最最基本和原始的树的弹出式菜单!!
	m_pContextMenu->LoadMenu(IDR_MENU_DBTREENODE);
}


//##ModelId=3C41296501DF
CDBTreeNode::CDBTreeNode() 
		:m_hItem(0),
		m_pContextMenu(NULL)
{
}
//##ModelId=3C41296501E9
CDBTreeNode::~CDBTreeNode()
{
	//在PopupContextMenu结束之后就应当释放掉这个
	//菜单,如果在对象析构的时候发现还没有释放掉这个
	//菜单,证明是有问题的。
	ASSERT(NULL == m_pContextMenu);
}

//##ModelId=3C41D26C00DC
void CDBTreeNode::OnMenuItemClick(UINT nCmd)
{
	if(ID_POPUP_DELETE == nCmd)
	{
 		this->Erase(this);
		return;
	}
	CString strMsg;
	strMsg.Format("%u\t没有写入实际的操作, "
		"目前您见到的这个是系统缺省的处理",
		nCmd);
	AfxMessageBox(strMsg, MB_OK | MB_ICONINFORMATION);
}

//##ModelId=3C41D26C0104
void CDBTreeNode::BuildTVITEM(HTREEITEM hParentItem)
{
	
	//创建与树的结点相关的信息,即TVINSERTSTRUCT
	ASSERT(hParentItem);


	TVINSERTSTRUCT tvInsert;
	tvInsert.hParent		= hParentItem;
	tvInsert.hInsertAfter	= TVI_SORT;
	tvInsert.item.mask		= TVIF_TEXT | TVIF_IMAGE 
		| TVIF_PARAM | TVIF_SELECTEDIMAGE
		| TVIF_CHILDREN;

	tvInsert.item.pszText	= (char *)GetTreeNodeCaption();

	tvInsert.item.iImage	= 
		tvInsert.item.iSelectedImage = 1;
	
	tvInsert.item.lParam	= (LPARAM) this;
	
	tvInsert.item.cChildren	= 1;
	m_tvInsertStruct = tvInsert;

}

//##ModelId=3C41D26C00B3
const char * CDBTreeNode::GetTreeNodeCaption()
{
	static char strCaption[] = "default caption";
	return strCaption;
}


//##ModelId=3C5B9B9F02C5
CDBExpDatabase *CDBTreeNode::GetDatabase()
{
	ASSERT(m_hItem);

	HTREEITEM  hItem = m_hItem;
	CDBExpDatabase **ppDatabase;
	CObject * pObject = NULL;
	while(hItem)
	{
		m_pTreeCtrl->AssertValid();

		pObject = (CObject *)m_pTreeCtrl->GetItemData(hItem);

		if(NULL != pObject)
		{
			if(pObject->IsKindOf(RUNTIME_CLASS(CDBExpAlias)))
			{
				ppDatabase = &((CDBExpAlias *)pObject)->m_pDatabase;
				goto FindDatabasePointer;
			}
		}

		hItem = m_pTreeCtrl->GetParentItem(hItem);
	}
	//不是一个正常的分支, 这是一个不正常的情况!!! 
	throw _T("不是一个正常的分支, 这是一个不正常的情况!!! ");
	return NULL;

FindDatabasePointer:
	if(NULL == *ppDatabase)
		*ppDatabase = new CDBExpDatabase(((CDBExpAlias *)pObject)->GetConnectString());

	HRESULT hr = S_OK;

	if(NULL == (*ppDatabase)->m_ptrConnection)
		hr = (*ppDatabase)->m_ptrConnection.CreateInstance(__uuidof(ADODB::Connection));
	try
	{
		if(FAILED(hr))
			_com_issue_error(hr);
	}
	catch(_com_error &e)
	{
		AfxMessageBox((const char *)e.Description());
	}
	return *ppDatabase;
}

//##ModelId=3C5B9B9F02BB
void CDBTreeNode::Set_hItem(HTREEITEM hItem)
{
	m_hItem = hItem;
}

//##ModelId=3C5B9B9F02B1
HTREEITEM CDBTreeNode::Get_hItem()
{
	return m_hItem;
}

//##ModelId=3C5CC9C70150
void CDBTreeNode::BuildLVITEM(int& nIndex)
{

	LVITEM		lvInsert;

	memset(&lvInsert, 0, sizeof(lvInsert));
	lvInsert.mask = LVIF_DI_SETITEM | LVIF_IMAGE
		| LVIF_PARAM | LVIF_TEXT;

	lvInsert.iItem		= nIndex ++;
	lvInsert.pszText	= (char *)GetTreeNodeCaption();

	lvInsert.iImage		= 0;
	
	lvInsert.lParam		= (LPARAM) this;
	
	m_lvInsertStruct	= lvInsert;

}



//##ModelId=3C5CC9C7013C
int& CDBTreeNode::ResetListViewItemCounter()
{
	static int nListViewItemCounter;

	return nListViewItemCounter = 0;
}


//##ModelId=3C673C070107
void CDBTreeNode::Erase(CDBTreeNode * pTreeNode)
{
	delete this;
}


//##ModelId=3CDCDA570203
char * CDBTreeNode::GetDatabaseType()
{
	CDBExpDatabase *pDatabase = GetDatabase();
	return pDatabase->GetDatabaseType();

}

⌨️ 快捷键说明

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