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

📄 dialogtab2.cpp

📁 MFC 空间数据的表达与操作
💻 CPP
字号:
// DialogTab2.cpp : implementation file
//

#include "stdafx.h"
#include "MyGeoMap.h"
#include "DialogTab2.h"

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

#include "MyGeoMapDoc.h"
#include "MyGeoMapView.h"
/////////////////////////////////////////////////////////////////////////////
// CDialogTab2 dialog


CDialogTab2::CDialogTab2(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogTab2::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogTab2)
	m_str_id = _T("");
	m_value = 1;
	m_tablename = _T("");
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_ICONSHOW);
	m_hIconHide = AfxGetApp()->LoadIcon(IDR_ICONHIDE);
	m_layernumber=0;
	BaseName="";
	ID="";
}


void CDialogTab2::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogTab2)
	DDX_Control(pDX, IDC_EDIT2, m_edit);
	DDX_Control(pDX, IDC_COMBO1, m_id);
	DDX_Control(pDX, IDC_TREE, m_tree);
	DDX_CBString(pDX, IDC_COMBO1, m_str_id);
	DDX_Text(pDX, IDC_EDIT2, m_value);
	DDX_Text(pDX, IDC_EDIT1, m_tablename);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogTab2, CDialog)
	//{{AFX_MSG_MAP(CDialogTab2)
	ON_WM_PAINT()
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnSelchangedTree)
	ON_NOTIFY(NM_RCLICK, IDC_TREE, OnRclickTree)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogTab2 message handlers

void CDialogTab2::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CRect rect;
	this->GetClientRect(&rect);
	m_tree.MoveWindow(CRect(0,0,rect.Width(),200));
		
	// Do not call CDialog::OnPaint() for painting messages
}

void CDialogTab2::AddLayers(CString name)
{
	TVINSERTSTRUCT tvInsert;
	HTREEITEM hParent;
	tvInsert.hParent = NULL;
	tvInsert.hInsertAfter = NULL;
	tvInsert.item.mask = TVIF_TEXT;
	m_tree.DeleteAllItems();
	
	BaseName=name;
	////////////连接数据库//////////////
	HRESULT hr;
	try
	{
		hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
		if(SUCCEEDED(hr))
		{
			CString strtemp;
			strtemp.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s",name);
			hr = m_pConnection->Open((_bstr_t)strtemp,"","",adModeUnknown);///连接数据库
			///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;  }
			m_pRecordset=m_pConnection->OpenSchema(adSchemaTables);
			CString tablename;
			CString tabletype;
			m_layernumber=0;
			
			while (!m_pRecordset->adoEOF) 
			{
				tablename=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("TABLE_NAME");
				tabletype=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("TABLE_TYPE");
				if (tabletype=="TABLE"&&tablename!="Index")
				{	
					m_layernumber++;
				}
				m_pRecordset->MoveNext();
			}
			layerinfo=new LayerInfo[m_layernumber];
			m_pRecordset->MoveFirst();
			int i=0;
			while (!m_pRecordset->adoEOF) 
			{
				tablename=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("TABLE_NAME");
				tabletype=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("TABLE_TYPE");
				if (tabletype=="TABLE"&&tablename!="Index")
				{	
					m_tree.SetImageList(&m_imageList,TVSIL_NORMAL); 
					hParent = m_tree.InsertItem(tablename);
					layerinfo[i++].layername=tablename;
					layerinfo[i-1].Visible=true;
					AddFields(hParent,tablename);
				}
				m_pRecordset->MoveNext();
			}
			m_pRecordset->Close();
		}
	}
	catch(_com_error e)///捕捉异常
	{
		CString errormessage;
		errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
		AfxMessageBox(errormessage);///显示错误信息
	}
	UpdateData(false);
}

BOOL CDialogTab2::AddFields(HTREEITEM hParent, CString tablename)
{
	HRESULT		hr;
	_bstr_t		mStrSQL;
	CString		strColName;
	BSTR		bstrColName;
	long		ColCount,i; 
	Field*		field = NULL;		
	Fields*		fields = NULL;
	LPCTSTR		nameField;
	_RecordsetPtr m_pRecordsetTemp;
	m_pRecordsetTemp.CreateInstance("ADODB.Recordset");
	// 打开记录集,得到字段名,并将字段名信息添加到ListBox中
	CString SQL;
	SQL.Format("SELECT * FROM %s",tablename);
	mStrSQL = SQL;
	
	m_pRecordsetTemp->Open(mStrSQL,               
		m_pConnection.GetInterfacePtr(),
		adOpenDynamic,
		adLockOptimistic,
		adCmdText);
	
	hr = m_pRecordsetTemp->get_Fields(&fields);	// 得到记录集的字段集和	
	
	if(SUCCEEDED(hr))
	fields->get_Count(&ColCount);
	
	// 得到记录集的字段集合中的字段的总个数
	for(i = 0; i < ColCount; i++)			
	{			
		fields->Item[i]->get_Name(&bstrColName);	// 得到记录集中的字段名		
		strColName = bstrColName;	
		nameField = strColName;
		HTREEITEM hChild=m_tree.InsertItem(strColName,hParent,NULL);		
	}
	
	if(SUCCEEDED(hr))
		fields->Release();				// 释放指针
	// 关闭记录集
	m_pRecordsetTemp->Close();
	return true;
}

void CDialogTab2::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here

	*pResult = 0;
}

BOOL CDialogTab2::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_tree.SetBkColor(RGB(247,247,255));
	m_tree.SetTextColor(RGB(0,0,255));
	m_imageList.Create(16,16,0,2,2);
	m_imageList.Add(m_hIcon); 
	m_imageList.Add(m_hIconHide); 

	m_id.SetCurSel(0);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDialogTab2::OnRclickTree(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	if (BaseName!=""){
		
		CMyGeoMapView *pView;
		pView=(CMyGeoMapView*)((CFrameWnd*)AfxGetMainWnd())->GetActiveView();
		
		hTreeCurrent=m_tree.GetSelectedItem ();
		hTreeParent=m_tree.GetParentItem(m_tree.GetSelectedItem ());
		
		CString layername;
		int i=0;
		layername=m_tree.GetItemText(hTreeCurrent);
		
		if (hTreeParent==NULL&&m_tree.GetCheck(hTreeCurrent)) 
		{
			for(i=0;i<m_layernumber;i++)
			{
				if (layerinfo[i].layername==layername) 
				{
					layerinfo[i].Visible=false;
				}
				m_tree.SetCheck(hTreeCurrent,false);
				m_tree.SetItemImage(hTreeCurrent,1,1);
			}			
		}
		else if(hTreeParent==NULL&&!m_tree.GetCheck(hTreeCurrent))
		{
			for(i=0;i<m_layernumber;i++)
			{
				if (layerinfo[i].layername==layername) 
				{
					layerinfo[i].Visible=true;
				}
				m_tree.SetCheck(hTreeCurrent,true);
				m_tree.SetItemImage(hTreeCurrent,0,0);
			}
			
			m_tree.SetCheck(hTreeCurrent,true);
			m_tree.SetItemImage(hTreeCurrent,0,0);
		}
		
		pView->DrawLayer(pView->m_zooom);
		pView->Invalidate();
	}
	*pResult = 0;
}

void CDialogTab2::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(true);
	int i=0;
	ID=m_str_id;
	if (ID!="")
	{
		CString SQl;
		_variant_t temp;
		double *doustemp;
		BOOL check=true;
		_RecordsetPtr m_pRecordsetTemp;
		m_pRecordsetTemp.CreateInstance("ADODB.Recordset");
		for(i=0;i<m_layernumber;i++)
		{
			SQl.Format("SELECT * FROM %s WHERE [%s]=%d ",layerinfo[i].layername,m_str_id,m_value);
			m_pRecordsetTemp->Open((_variant_t)SQl,_variant_t(m_pConnection,true)
				,adOpenStatic,adLockOptimistic,adCmdText);	
			if (!m_pRecordsetTemp->adoEOF) 
			{
				CMyGeoMapView *pView;
				pView=(CMyGeoMapView*)((CFrameWnd*)AfxGetMainWnd())->GetActiveView();
				long nLength=m_pRecordsetTemp->Fields->GetItem("Corser")->ActualSize;
				temp=m_pRecordsetTemp->GetFields()->GetItem("CorSer")->GetChunk(nLength);
				double *pBuf = NULL;
				doustemp=new double[nLength/8];
				SafeArrayAccessData(temp.parray,(void**)&pBuf);
				memcpy(doustemp,pBuf,nLength);
				SafeArrayUnaccessData (temp.parray);
				pView->DrawSelectObject(layerinfo[i].layername,doustemp,nLength/16);
				m_tablename=layerinfo[i].layername;
				i=m_layernumber;
				check=false;
			}
			m_pRecordsetTemp->Close();
		}
		if (check) 
		{
			MessageBox("所找的地物不存在!");
			m_edit.SetFocus();
		}	
		UpdateData(false);
	}
	else MessageBox("请选择ID种类!");
	//	CDialog::OnOK();
}

void CDialogTab2::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	if (m_pConnection!=NULL) 
	{
		m_pConnection->Close();
	}
	CDialog::OnClose();
}

⌨️ 快捷键说明

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