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

📄 dialogtab1.cpp

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

#include "stdafx.h"
#include "MyGeoMap.h"
#include "DialogTab1.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

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


CDialogTab1::CDialogTab1(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogTab1::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogTab1)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	Layers=NULL;
	m_pMemDC      = new CDC;
	m_pMemBitmap  = new CBitmap;
	m_zooom_view=0.0001;
}


void CDialogTab1::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogTab1)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogTab1, CDialog)
	//{{AFX_MSG_MAP(CDialogTab1)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogTab1 message handlers

void CDialogTab1::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CBrush pBrushNew;
	CPen penNew;
	pBrushNew.CreateSolidBrush(RGB(255,255,255));
	dc.FillRect(rect,&pBrushNew);
	pBrushNew.DeleteObject();
	if (tablename!="") 
	{	
		dc.BitBlt(0, 0,
			rect.Width(),
			rect.Height(),
			m_pMemDC,
			0,
			0,
			SRCCOPY);	
		if (penNew.CreatePen(BS_SOLID,2,RGB(255,0,0)))
		{
			double i=rect.Width()/m_zooom_eagletemp*m_zooom_view/2;
			double j=rect.Height()/m_zooom_eagletemp*m_zooom_view/2;
			dc.SelectObject(&penNew);
			dc.MoveTo((int)(viewcenterx-i),(int)(viewcentery-j));
			dc.LineTo((int)(viewcenterx-i),(int)(viewcentery+j));
			dc.LineTo((int)(viewcenterx+i),(int)(viewcentery+j));
			dc.LineTo((int)(viewcenterx+i),(int)(viewcentery-j));
			dc.LineTo((int)(viewcenterx-i),(int)(viewcentery-j));	
		}		
	}

}

void CDialogTab1::GetDefaultData(double x1, double y1, double x2, double y2,CString table,double zooom)
{
	mapcenterx=(x1+x2)/2;
	mapcentery=(y1+y2)/2;
	if (rect.Width()/(x2-x1)<rect.Height()/(y2-y1)) 
	{
		m_zooom_eagle=rect.Width()/(x2-x1);
	}
	else
	{
		m_zooom_eagle=rect.Height()/(y2-y1);		
	}
	m_zooom_view=zooom;
	tablename=table;
	InitDataBase(tablename);
	Layers=((CMainFrame *)AfxGetMainWnd())->mybar.tab2;
	UpdateData(false);
	DrawLayer(m_zooom_eagle);
	Invalidate();
}

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

void CDialogTab1::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;
	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++)
	{
		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,1,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));
				}
				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);	
	}	
}

void CDialogTab1::InitDataBase(CString tablename)
{
	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",tablename);
			hr = m_pConnection->Open((_bstr_t)strtemp,"","",adModeUnknown);///连接数据库
			///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;  }
			
		}
	}
	catch(_com_error e)///捕捉异常
	{
		CString errormessage;
		errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
		AfxMessageBox(errormessage);///显示错误信息
	}
}

BOOL CDialogTab1::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	GetClientRect(&rect);
	CClientDC pDC(this);
	if (m_pMemBitmap!=NULL&&m_pMemDC!=NULL) 
	{
		m_pMemDC->DeleteDC();
		m_pMemBitmap->DeleteObject();
		m_pMemDC->CreateCompatibleDC(&pDC);
		m_pMemBitmap->CreateCompatibleBitmap(&pDC,rect.Width()-1,rect.Height()-1);	
		m_pMemDC->SelectObject(m_pMemBitmap);
	}
	else
	{
		m_pMemDC->CreateCompatibleDC(&pDC);
		m_pMemBitmap->CreateCompatibleBitmap(&pDC,rect.Width()-1,rect.Height()-1);	
		m_pMemDC->SelectObject(m_pMemBitmap);
	}
	CBrush bush;
	bush.CreateSolidBrush(RGB(255,255,255));
	m_pMemDC->FillRect(rect,&bush);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDialogTab1::GetCurrentCenter(double x, double y, double zooom)
{
	CPoint point;
	point=this->MapToScreen(mapcenterx,mapcentery,x,y,m_zooom_eagle);
	if (viewcenterx!=point.x||viewcentery!=point.y)
	{	
		viewcenterx=point.x;
		viewcentery=point.y;
		m_zooom_eagletemp=zooom;
		UpdateData(false);
		Invalidate();
	}
}

void CDialogTab1::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (tablename!="") 
	{	
		CMyGeoMapView *pView;
		pView=(CMyGeoMapView*)((CFrameWnd*)AfxGetMainWnd())->GetActiveView();
		double x,y;
		this->ScreenToMap(point,&x,&y);
		pView->SetCenterPoint(x,y);
		CDialog::OnLButtonDown(nFlags, point);
	}
}

void CDialogTab1::ScreenToMap(CPoint point, double *x, double *y)
{
	*x=(point.x-rect.Width()/2)/m_zooom_eagle+mapcenterx;
	*y=mapcentery-(point.y-rect.Height()/2)/m_zooom_eagle;
}

void CDialogTab1::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 + -