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

📄 mygeomapview.cpp

📁 MFC 空间数据的表达与操作
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MyGeoMapView.cpp : implementation of the CMyGeoMapView class
//

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

#include "MyGeoMapDoc.h"
#include "MyGeoMapView.h"
#include "MainFrm.h"
#include "math.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyGeoMapView

IMPLEMENT_DYNCREATE(CMyGeoMapView, CView)

BEGIN_MESSAGE_MAP(CMyGeoMapView, CView)
//{{AFX_MSG_MAP(CMyGeoMapView)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_ZOOOM_IN, OnZooomIn)
ON_COMMAND(ID_ZOOOM_OUT, OnZooomOut)
ON_WM_SETCURSOR()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_FULLSIZE, OnFullsize)
ON_COMMAND(ID_MOVE, OnMove)
	ON_COMMAND(ID_REDO, OnRedo)
	//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyGeoMapView construction/destruction

CMyGeoMapView::CMyGeoMapView()
{
	// TODO: add construction code here
	SelectTool=-1;
	m_pMemDC      = new CDC;
	m_pMemBitmap  = new CBitmap;
	Bitmapx=Bitmapy=0;
	SelectPointNumber=0;
	pointselect=new double[1];

	m_pConnection = NULL;
}

CMyGeoMapView::~CMyGeoMapView()
{
	delete m_pMemDC;
	delete m_pMemBitmap;
	if (m_pConnection!=NULL) 
	{
		m_pConnection->Close();
	}
	if (m_pRecordset!=NULL) 
	{
		m_pRecordset->Close();
	}
}

BOOL CMyGeoMapView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyGeoMapView drawing
void CMyGeoMapView::OnDraw(CDC* pDC)
{
	CMyGeoMapDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CRect clientRect;
	GetClientRect(&clientRect);	
	if (DataBaseName!="")
	{
		IfViewChange(clientRect);
		
		pDC->BitBlt(0, 0,
			clientRect.Width(),
			clientRect.Height(),
			m_pMemDC,
			Bitmapx,
			Bitmapy,
			SRCCOPY);
		
		DrawSelect(pDC,SelectPointNumber,SelectName);
		
		Eagle->GetCurrentCenter(mapcenterx,mapcentery,m_zooom);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMyGeoMapView printing

BOOL CMyGeoMapView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMyGeoMapView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMyGeoMapView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMyGeoMapView diagnostics

#ifdef _DEBUG
void CMyGeoMapView::AssertValid() const
{
	CView::AssertValid();
}

void CMyGeoMapView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMyGeoMapDoc* CMyGeoMapView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyGeoMapDoc)));
	return (CMyGeoMapDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyGeoMapView message handlers

void CMyGeoMapView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"MDB File(*.mdb)|*.mdb|\
		所有文件(*.*)|*.*||"
		);				
	if(dlg.DoModal()==IDOK)
	{
		
		if (dlg.GetFileExt()=="mdb") 
		{				
			DataBaseName=dlg.GetPathName();	
			OpenDataBase(DataBaseName);
		}
		else 
			MessageBox("数据库格式错误!");
		
		Layers=((CMainFrame *)AfxGetMainWnd())->mybar.tab2;
		Eagle=((CMainFrame *)AfxGetMainWnd())->mybar.tab1;	
		GetCenterPoint();
		Eagle->GetDefaultData(mapcenterxMin,mapcenteryMin,mapcenterxMax,mapcenteryMax,DataBaseName,m_zooom);
		DrawLayer(m_zooom);
		Invalidate();
	}
	
}

void CMyGeoMapView::OpenDataBase(CString 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;  }
			
		}
	}
	catch(_com_error e)///捕捉异常
	{
		DataBaseName="";
		CString errormessage;
		errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
		AfxMessageBox(errormessage);///显示错误信息
	}
	((CMainFrame *)AfxGetMainWnd())->mybar.tab2->AddLayers(Name);
	((CMainFrame *)AfxGetMainWnd())->mybar.tab2->m_id.SetCurSel(0);
	
}

void CMyGeoMapView::DrawLayer(double zooom)
{
	_RecordsetPtr m_pRecordsetTemp;
	m_pRecordsetTemp.CreateInstance("ADODB.Recordset");
	
	hTreeCurrent=Layers->m_tree.GetFirstVisibleItem();
	hTreeParent=Layers->m_tree.GetParentItem(hTreeCurrent);
	_variant_t temp,ObjectID;

	CPoint *point;	
	double *doustemp;
	
	CPen  *pPenOld, PenNew;
	CBrush *pBrushOld,pBrushNew;
	
	pBrushNew.CreateSolidBrush(RGB(255,255,255));
	m_pMemDC->FillRect(rect,&pBrushNew);
	pBrushNew.DeleteObject();
	
	for(int i=0;i<Layers->m_layernumber&&hTreeParent==NULL;i++)
	{
		if (Layers->layerinfo[i].Visible) 
		{
			CString SQl;
			SQl.Format("SELECT * FROM %s",Layers->m_tree.GetItemText(hTreeCurrent));
			m_pRecordsetTemp->Open((_variant_t)SQl,_variant_t(m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
			while (!m_pRecordsetTemp->adoEOF) 
			{
				long nLength=m_pRecordsetTemp->Fields->GetItem("Corser")->ActualSize;
				temp=m_pRecordsetTemp->GetFields()->GetItem("CorSer")->GetChunk(nLength);
				double *pBuf = NULL;
				point=new CPoint[nLength/16];
				doustemp=new double[nLength/8];
				SafeArrayAccessData(temp.parray,(void**)&pBuf);
				memcpy(doustemp,pBuf,nLength);
				SafeArrayUnaccessData (temp.parray);
				for(int a=0;a<nLength/16;a++)
				{
					point[a]=MapToScreen(mapcenterx,mapcentery,doustemp[2*a],doustemp[2*a+1],zooom);
				}
				if (Layers->m_tree.GetItemText(hTreeCurrent)=="Line"&&PenNew.CreatePen(BS_SOLID,2,RGB(255,0,0)))
				{
					pPenOld=m_pMemDC->SelectObject(&PenNew);
					m_pMemDC->Polyline(point,nLength/16);
					m_pMemDC->SelectObject(pPenOld);
					PenNew.DeleteObject();
				}
				else if (Layers->m_tree.GetItemText(hTreeCurrent)=="Area"&&PenNew.CreatePen(BS_SOLID,1,RGB(0,255,0))) 
				{
					int str=(m_pRecordsetTemp->GetCollect("AreaID")).lVal;
					if (str%4==0) 
					{
						pBrushNew.CreateSolidBrush(RGB(128,128,0));
					}
					else if (str%4==1) 
					{
						pBrushNew.CreateSolidBrush(RGB(128,0,128));
					}
					else if (str%4==2) 
					{
						pBrushNew.CreateSolidBrush(RGB(0,128,128));
					}
					else if (str%4==3) 
					{
						pBrushNew.CreateSolidBrush(RGB(0,0,255));
					}
//					pBrushNew.CreateSolidBrush(RGB(0,0,255));
					pPenOld=m_pMemDC->SelectObject(&PenNew);
					pBrushOld=m_pMemDC->SelectObject(&pBrushNew);
					
					m_pMemDC->Polygon(point,nLength/16);
					
					m_pMemDC->SelectObject(pPenOld);
					m_pMemDC->SelectObject(pBrushOld);
					pBrushNew.DeleteObject();
					PenNew.DeleteObject();
				}
				
				delete []point;
				delete []doustemp;
				m_pRecordsetTemp->MoveNext();
			}
			m_pRecordsetTemp->Close();
		}
		hTreeCurrent=Layers->m_tree.GetNextItem(hTreeCurrent,1);	
		hTreeParent=Layers->m_tree.GetParentItem(hTreeCurrent);	
		
	}	
}

BOOL CMyGeoMapView::GetCenterPoint()
{
	mapcenterxMin=1024;
	mapcenteryMin=800;
	mapcenterxMax=0;
	mapcenteryMax=0;
	_variant_t temp;
	
	_RecordsetPtr m_pRecordsetTemp;
	m_pRecordsetTemp.CreateInstance("ADODB.Recordset");
	
	hTreeCurrent=Layers->m_tree.GetFirstVisibleItem();
	hTreeParent=Layers->m_tree.GetParentItem(hTreeCurrent);
	
	double *doustemp;
	
	for(int i=0;i<Layers->m_layernumber&&hTreeParent==NULL;i++)
	{
		CString str=Layers->m_tree.GetItemText(hTreeCurrent);
		CString SQl;
		SQl.Format("SELECT * FROM %s",str);
		m_pRecordsetTemp->Open((_variant_t)SQl,_variant_t(m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
		while (!m_pRecordsetTemp->adoEOF) 
		{
			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);
			for(int a=0;a<nLength/16;a++)
			{
				if (mapcenterxMin>doustemp[2*a]) 
				{
					mapcenterxMin=doustemp[2*a];
				}
				else if (mapcenterxMax<doustemp[2*a]) 
				{
					mapcenterxMax=doustemp[2*a];
				}
				if (mapcenteryMin>doustemp[2*a+1]) 
				{
					mapcenteryMin=doustemp[2*a+1];
				}
				else if (mapcenteryMax<doustemp[2*a+1]) 
				{
					mapcenteryMax=doustemp[2*a+1];
				}
			}
			delete []doustemp;
			m_pRecordsetTemp->MoveNext();
		}
		m_pRecordsetTemp->Close();
		hTreeCurrent=Layers->m_tree.GetNextItem(hTreeCurrent,1);	
		hTreeParent=Layers->m_tree.GetParentItem(hTreeCurrent);	
	}
	
	mapcenterx=(mapcenterxMax+mapcenterxMin)/2;
	mapcentery=(mapcenteryMax+mapcenteryMin)/2;
	
	if (((mapcenterx-mapcenterxMin)/rect.Width()/2)>((mapcentery-mapcenteryMin)/rect.Height()/2)) 
	{
		m_zooom=rect.Width()/(mapcenterx-mapcenterxMin)/2;
	}
	else
		m_zooom=rect.Height()/(mapcentery-mapcenteryMin)/2;
	return true;
}

CPoint CMyGeoMapView::MapToScreen(double centerx, double centery, double pointx,double pointy, double zooom_in_out)
{
	CPoint point;
	point.x=(long)((pointx-centerx)*m_zooom+rect.Width()/2);
	point.y=(long)((centery-pointy)*m_zooom+rect.Height()/2);
	return point;
}

void CMyGeoMapView::OnZooomIn() 
{
	// TODO: Add your command handler code here
	if (DataBaseName!="") 
	{
		SelectTool=0;
		UpdateData(false);
	}	
	
}

void CMyGeoMapView::OnZooomOut() 
{
	// TODO: Add your command handler code here
	if (DataBaseName!="") 
	{
		SelectTool=1;
		UpdateData(false);
	}	
	
}

BOOL CMyGeoMapView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	switch(SelectTool)
	{
	case 0:
	case 10:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR4));
		break;
	case 1:
	case 11:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR3));
		break;
	case 2:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR2));
		break;
	case 3:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR1));
		break;
	default:
		::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
	}
	return true;
}

void CMyGeoMapView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (DataBaseName!="")
	{
		switch(SelectTool) {
		case -1:
			DrawCurrentObject(point);
			break;
		case 0:
			pointstart=pointend=point;
			SelectTool=10;
			break;
		case 1:
			pointstart=pointend=point;
			SelectTool=11;
			break;
		case 2:
			pointstart=pointmove=point;
			::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR1));
			SelectTool=3;
			break;
		case 3:
			break;
		}
	}

	CView::OnLButtonDown(nFlags, point);
}

void CMyGeoMapView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int a,b;
	switch(SelectTool) {
	case 10:
		SelectTool=0;
		pointend=point;
		DrawLine(FALSE);
		a=point.x-pointstart.x;
		b=point.y-pointstart.y;
		if ((a<3&&a>-3)||(b<3&&b>-3)) 
		{
			mapcenterx+=(point.x-rect.Width()/2)/m_zooom;
			mapcentery-=(point.y-rect.Height()/2)/m_zooom;
			m_zooom=m_zooom*1.3;
			DrawLayer(m_zooom);
			Invalidate();
		}
		else
		{
			mapcenterx+=((point.x+pointstart.x)/2-rect.Width()/2)/m_zooom;
			mapcentery-=((point.y+pointstart.y)/2-rect.Height()/2)/m_zooom;
			if (a*a*1.0/(rect.Width()*rect.Width())<1.0*b*b/(rect.Height()*rect.Height())) 
			{
				if (a>0) 
				{
					m_zooom*=rect.Height()*1.0/a;
				}

⌨️ 快捷键说明

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