📄 mifview.cpp
字号:
// MIFView.cpp : implementation of the CMIFView class
//
#include "stdafx.h"
#include "MIF.h"
#include "MIFDoc.h"
#include "MIFView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMIFView
IMPLEMENT_DYNCREATE(CMIFView, CView)
BEGIN_MESSAGE_MAP(CMIFView, CView)
//{{AFX_MSG_MAP(CMIFView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMIFView construction/destruction
CMIFView::CMIFView()
{
// TODO: add construction code here
num_pt_type = 0;
num_ln_type = 0;
num_pg_type = 0;
pt_type = NULL;
ln_type = NULL;
pg_type = NULL;
}
CMIFView::~CMIFView()
{
}
BOOL CMIFView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMIFView drawing
void CMIFView::OnDraw(CDC* pDC)
{
//////////注意,在OnDraw中不要调用AfxMessageBox()函数,否则引起死循环
CMIFDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//定义循环变量
int i,j,k,l;
//获得当前客户区的矩形区域
CRect rc;
GetClientRect(&rc);
//当前客户区的矩形区域的y坐标长度间隔:rc.Height();
//当前客户区的矩形区域的x坐标长度间隔:rc.Width();
//准备GDI设备
//准备画笔
CPen pen1,pen2;
//pen1.CreatePen(PS_SOLID,2,RGB(255,0,0)); //点 :红色
pen1.CreatePen(PS_SOLID,1,RGB(0,255,0)); //线 :绿色
pen2.CreatePen(PS_SOLID,1,RGB(0,0,255)); //多边形 :蓝色
// CBrush brush;
// brush.CreateSolidBrush(RGB(0,128,255)); //多边形填充画刷
//定义屏幕绘图坐标
CPoint point1,point2;
if(pDoc->redraw)
{
//点,线,多边形数据引入
num_pt_type = pDoc->num_pt_type;
num_ln_type = pDoc->num_ln_type;
num_pg_type = pDoc->num_pg_type;
pt_type = pDoc->pt_type;
ln_type = pDoc->ln_type;
pg_type = pDoc->pg_type;
//图形显示的预处理操作(坐标重心化),使得图形居中显示
//计算读入文件中点坐标(x,y)的最大值,最小值x_max,y_max,x_min,y_min
//1.计算点文件的点坐标(x,y)的最大值,最小值pt_x_max,pt_y_maxpt_x_min,pt_y_min
float pt_x_max ,pt_y_max ,pt_x_min ,pt_y_min;
if(pt_type)
{
//赋初值
pt_x_max = pt_x_min =pt_type[0].pt[0].lat;
pt_y_max = pt_y_min =pt_type[0].pt[0].lon;
//循环处理
for(i = 0; i < num_pt_type; i++)
for(j = 0; j < pt_type[i].num_pt; j++)
{
if(pt_type[i].pt[j].lat < pt_x_min)
{
pt_x_min = pt_type[i].pt[j].lat;
}
else if(pt_type[i].pt[j].lat > pt_x_max)
{
pt_x_max = pt_type[i].pt[j].lat;
}
if(pt_type[i].pt[j].lon < pt_y_min)
{
pt_y_min = pt_type[i].pt[j].lon;
}
else if(pt_type[i].pt[j].lon > pt_y_max)
{
pt_y_max = pt_type[i].pt[j].lon;
}
}
}
// CString str1;
// str1.Format("pt_x_max = %f,pt_y_max = %f,pt_x_min = %f, pt_y_min = %f",pt_x_max,pt_y_max,pt_x_min,pt_y_min);
// AfxMessageBox(str1);
//2.计算线文件的点坐标(x,y)的最大值,最小值ln_x_max,ln_y_max,ln_x_min,ln_y_min
float ln_x_max ,ln_y_max ,ln_x_min ,ln_y_min;
if(ln_type)
{
//赋初值
ln_x_max = ln_x_min =ln_type[0].ln[0].pt[0].lat;
ln_y_max = ln_y_min =ln_type[0].ln[0].pt[0].lon;
//循环处理
for(i = 0; i < num_ln_type; i++)
for(j = 0; j < ln_type[i].num_ln; j++)
for(k = 0; k <ln_type[i].ln[j].num_pt;k++ )
{
if(ln_type[i].ln[j].pt[k].lat < ln_x_min)
{
ln_x_min = ln_type[i].ln[j].pt[k].lat;
}
else if(ln_type[i].ln[j].pt[k].lat > ln_x_max)
{
ln_x_max = ln_type[i].ln[j].pt[k].lat;
}
if(ln_type[i].ln[j].pt[k].lon < ln_y_min)
{
ln_y_min = ln_type[i].ln[j].pt[k].lon;
}
else if(ln_type[i].ln[j].pt[k].lon > ln_y_max)
{
ln_y_max = ln_type[i].ln[j].pt[k].lon;
}
}
}
// CString str1;
// str1.Format("ln_x_max = %f,ln_y_max = %f,ln_x_min = %f, ln_y_min = %f",ln_x_max,ln_y_max,ln_x_min,ln_y_min);
// AfxMessageBox(str1);
//3.计算多边形文件的点坐标(x,y)的最大值,最小值pg_x_max,pg_y_max,pg_x_min,pg_y_min
float pg_x_max ,pg_y_max ,pg_x_min ,pg_y_min;
if(pg_type)
{
//赋初值
pg_x_max = pg_x_min =pg_type[0].pg[0].pt[0].lat;
pg_y_max = pg_y_min =pg_type[0].pg[0].pt[0].lon;
//循环处理
for(i = 0; i < num_pg_type; i++)
for(j = 0; j < pg_type[i].num_pg; j++)
for(k = 0; k <pg_type[i].pg[j].num_pt;k++ )
{
if(pg_type[i].pg[j].pt[k].lat < pg_x_min)
{
pg_x_min = pg_type[i].pg[j].pt[k].lat;
}
else if(pg_type[i].pg[j].pt[k].lat > pg_x_max)
{
pg_x_max = pg_type[i].pg[j].pt[k].lat;
}
if(pg_type[i].pg[j].pt[k].lon < pg_y_min)
{
pg_y_min = pg_type[i].pg[j].pt[k].lon;
}
else if(pg_type[i].pg[j].pt[k].lon > pg_y_max)
{
pg_y_max = pg_type[i].pg[j].pt[k].lon;
}
}
}
//CString str1;
// str1.Format("pg_x_max = %f,pg_y_max = %f,pg_x_min = %f, pg_y_min = %f",pg_x_max,pg_y_max,pg_x_min,pg_y_min);
// AfxMessageBox(str1);
//4.比较点,线,多边形的点坐标(x,y)的最大值,最小值,得到x_max,y_max,x_min,y_min
float x_max ,y_max ,x_min ,y_min;
x_max = pt_x_max;
y_max = pt_y_max;
x_min = pt_x_min;
y_min = pt_y_min;
if(ln_x_min < x_min)
{
x_min = ln_x_min;
}
else if(pg_x_min < x_min)
{
x_min = pg_x_min;
}
if(ln_y_min < y_min)
{
y_min = ln_y_min;
}
else if(pg_y_min < y_min)
{
y_min = pg_y_min;
}
if(ln_x_max > x_max)
{
x_max = ln_x_max;
}
else if(pg_x_max > x_max)
{
x_max = pg_x_max;
}
if(ln_y_max > y_max)
{
y_max = ln_y_max;
}
else if(pg_y_max > y_max)
{
y_max = pg_y_max;
}
//CString str1;
//str1.Format("x_max = %f,y_max = %f,x_min = %f, y_min = %f",x_max,y_max,x_min,y_min);
//AfxMessageBox(str1);
//5.计算重心坐标和x,y轴缩放因子Mx,My
float delta_x,delta_y,Mx,My;
delta_x = (x_max - x_min);
delta_y = (y_max - y_min);
Mx = rc.Width()/delta_x;
My = rc.Height()/delta_y;
// CString str1;
// str1.Format("delta_x = %f,delta_y = %f,Mx = %f, My = %f",delta_x,delta_y,Mx,My);
// AfxMessageBox(str1);
//6.按照坐标重心化计算结果开始绘制图形
//使用红色画笔,画出点数据!!!
if(pt_type)
{
//记录循环数目
k = 0;
for(i = 0; i < num_pt_type;i++)
for(j = 0; j < pt_type[i].num_pt; j++)
{
point1.x = (int)((pt_type[i].pt[j].lat-x_min)*Mx);
point1.y = (int)(rc.Height()-(pt_type[i].pt[j].lon-y_min)*My);
// point1.x = (int)((pt_type[i].pt[j].lat-x_min)*Mx);
// point1.y = (int)(rc.Height()-(pt_type[i].pt[j].lon-y_min)*My);
pDC->SetPixel(point1,RGB(255,0,0));
//计算总点数
k++;
}
CString str;
str.Format("mif点文件中总共有%d个点",k);
pDC->TextOut(0,rc.Height()-60,str);
}
//使用绿色画笔,画出线数据!!!
if(ln_type)
{
CPen *pPenOld=(CPen*)pDC->SelectObject(&pen1);
//记录循环数目
l = 0;
for(i = 0; i < num_ln_type;i++)
for(j = 0; j < ln_type[i].num_ln; j++)
{
//point1指向第一个数据点
point1.x = (int)((ln_type[i].ln[j].pt[0].lat-x_min)*Mx);
point1.y = (int)(rc.Height()-(ln_type[i].ln[j].pt[0].lon-y_min)*My);
//画笔移到第一个数据点
pDC->MoveTo(point1);
for(k = 1; k < ln_type[i].ln[j].num_pt; k++)
{
//使用point2记录当前数据点
point2.x = (int)((ln_type[i].ln[j].pt[k].lat-x_min)*Mx);
point2.y = (int)(rc.Height()-(ln_type[i].ln[j].pt[k].lon-y_min)*My);
//画笔画到当前记录数据点
pDC->LineTo(point2);
//计算总线数
l++;
}
}
pDC->SelectObject(pPenOld);
CString str;
str.Format("mif线文件中总共有%d个点",l);
pDC->TextOut(0,rc.Height()-40,str);
}
//使用蓝色画笔,画出多边形数据!!!
if(pg_type)
{
CPen *pPenOld=(CPen*)pDC->SelectObject(&pen2);
//记录循环数目
l = 0;
for(i = 0; i < num_pg_type; i++) //循环num_pg_type次
for(j = 0; j < pg_type[i].num_pg; j++) //循环pg_type[i].num_pg次
{
//point1指向第一个数据点
point1.x = (int)((pg_type[i].pg[j].pt[0].lat-x_min)*Mx);
point1.y = (int)(rc.Height()-(pg_type[i].pg[j].pt[0].lon-y_min)*My);
//画笔移到第一个数据点
pDC->MoveTo(point1);
for(k = 1; k < pg_type[i].pg[j].num_pt; k++)
{ //循环pg_type[i].pg[j].num_pt次
//使用point2记录当前数据点
point2.x = (int)((pg_type[i].pg[j].pt[k].lat-x_min)*Mx);
point2.y = (int)(rc.Height()-(pg_type[i].pg[j].pt[k].lon-y_min)*My);
//画笔画到当前记录数据点
pDC->LineTo(point2);
//计算总多边形数
l++;
}
}
pDC->SelectObject(pPenOld);
CString str;
str.Format("mif多边形文件中总共有%d个点",l);
pDC->TextOut(0,rc.Height()-20,str);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CMIFView printing
BOOL CMIFView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMIFView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMIFView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMIFView diagnostics
#ifdef _DEBUG
void CMIFView::AssertValid() const
{
CView::AssertValid();
}
void CMIFView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMIFDoc* CMIFView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMIFDoc)));
return (CMIFDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMIFView message handlers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -