www.pudn.com > DEM_TDS.rar > DEMView.cpp, change:2007-07-16,size:30463b


// DEMView.cpp : implementation of the CDEMView class 
// 
 
#include "stdafx.h" 
#include "DEM.h" 
 
#include "DEMDoc.h" 
#include "DEMView.h" 
#include "MainFrm.h" 
#include "math.h" 
#include "OpenGLText.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CDEMView 
 
IMPLEMENT_DYNCREATE(CDEMView, CGLEnabledView) 
 
BEGIN_MESSAGE_MAP(CDEMView, CGLEnabledView) 
	//{{AFX_MSG_MAP(CDEMView) 
	ON_WM_MOUSEWHEEL() 
	ON_COMMAND(ID_ZOOMIN, ZoomIn) 
	ON_COMMAND(ID_ZOOMOUT, ZoomOut) 
	ON_COMMAND(ID_REFLESH, ReFlesh) 
	ON_COMMAND(ID_ONCLICKENLARGE, OnClickEnlarge) 
	ON_COMMAND(ID_MouseDrag, OnMouseDrag) 
	ON_UPDATE_COMMAND_UI(ID_ZOOMIN, OnUpdateZoomin) 
	ON_UPDATE_COMMAND_UI(ID_ZOOMOUT, OnUpdateZoomout) 
	ON_UPDATE_COMMAND_UI(ID_REFLESH, OnUpdateReflesh) 
	ON_UPDATE_COMMAND_UI(ID_ONCLICKENLARGE, OnUpdateClickEnlarge) 
	ON_UPDATE_COMMAND_UI(ID_MouseDrag, OnUpdateMouseDrag) 
	ON_WM_SETCURSOR() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_MOUSEMOVE() 
	ON_WM_RBUTTONUP() 
	ON_WM_LBUTTONUP() 
	ON_WM_RBUTTONDOWN() 
	ON_UPDATE_COMMAND_UI(IDC_SHOWSCALE, OnUpdateShowscale) 
	ON_UPDATE_COMMAND_UI(IDC_MAPCOLOR, OnUpdateMapColor) 
	ON_WM_SIZE() 
	ON_COMMAND(ID_PICK, OnPick) 
	ON_UPDATE_COMMAND_UI(ID_PICK, OnUpdatePick) 
	ON_COMMAND(ID_SHOWFRAME, OnShowframe) 
	ON_UPDATE_COMMAND_UI(ID_SHOWFRAME, OnUpdateShowframe) 
	ON_COMMAND(ID_COLOR1, OnColor1) 
	ON_UPDATE_COMMAND_UI(ID_COLOR1, OnUpdateColor1) 
	ON_COMMAND(ID_COLOR2, OnColor2) 
	ON_UPDATE_COMMAND_UI(ID_COLOR2, OnUpdateColor2) 
	ON_WM_ERASEBKGND() 
	ON_COMMAND(ID_2D, On2d) 
	ON_UPDATE_COMMAND_UI(ID_2D, OnUpdate2d) 
	ON_COMMAND(ID_3D, On3d) 
	ON_UPDATE_COMMAND_UI(ID_3D, OnUpdate3d) 
	ON_COMMAND(ID_MOVING, OnMoving) 
	ON_UPDATE_COMMAND_UI(ID_MOVING, OnUpdateMoving) 
	ON_WM_KEYDOWN() 
	ON_WM_TIMER() 
	ON_COMMAND(ID_AUTOFLY, OnAutofly) 
	ON_UPDATE_COMMAND_UI(ID_AUTOFLY, OnUpdateAutofly) 
	ON_COMMAND(ID_AUTOHEIGHT, OnAutoheight) 
	ON_UPDATE_COMMAND_UI(ID_AUTOHEIGHT, OnUpdateAutoheight) 
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout) 
	ON_COMMAND(ID_STOP, OnStop) 
	ON_UPDATE_COMMAND_UI(ID_STOP, OnUpdateStop) 
	ON_COMMAND(ID_KEYA, OnKeya) 
	ON_COMMAND(ID_KEYADD, OnKeyadd) 
	ON_COMMAND(ID_KEYC, OnKeyc) 
	ON_COMMAND(ID_KEYD, OnKeyd) 
	ON_COMMAND(ID_KEYDOWN, OnKeydown) 
	ON_COMMAND(ID_KEYE, OnKeye) 
	ON_COMMAND(ID_KEYF, OnKeyf) 
	ON_COMMAND(ID_KEYQ, OnKeyq) 
	ON_COMMAND(ID_KEYR, OnKeyr) 
	ON_COMMAND(ID_KEYREDUSE, OnKeyreduse) 
	ON_COMMAND(ID_KEYS, OnKeys) 
	ON_COMMAND(ID_KEYUP, OnKeyup) 
	ON_COMMAND(ID_KEYW, OnKeyw) 
	ON_COMMAND(ID_KEYX, OnKeyx) 
	ON_COMMAND(ID_SMOOTH, OnSmooth) 
	ON_UPDATE_COMMAND_UI(ID_SMOOTH, OnUpdateSmooth) 
	ON_COMMAND(ID_SETFLYLIST, OnSetflylist) 
	ON_UPDATE_COMMAND_UI(ID_SETFLYLIST, OnUpdateSetflylist) 
	ON_COMMAND(ID_SHOWPATH, OnShowpath) 
	ON_UPDATE_COMMAND_UI(ID_SHOWPATH, OnUpdateShowpath) 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDEMView construction/destruction 
 
CDEMView::CDEMView() 
{ 
	// TODO: add construction code here 
///////////////////////////////////////////////////////////////////////////// 
//初始化成员变量 
	clickdrag=false; 
	clickenlarge=false; 
	mousepick=true; 
	maploadok=false; 
	mousedrag=false; 
	buttondown=false; 
	enlarge=false; 
	showframe=false; 
	showskirt=true; 
	autofly=false; 
	stop=true; 
	showpath=true; 
	inittoolbar=true; 
	setflylist=false; 
	demdata=NULL; 
	MemDC=NULL; 
	Az=0; 
	El=0; 
	speed=1; 
	earthrotate=0; 
	moonrotate=0; 
	bothrotate=180; 
////////////////////////////////////////////////////////////////////////////// 
} 
 
CDEMView::~CDEMView() 
{ 
	MemBitmap.DeleteObject();  
	if(MemDC) 
	{ 
		MemDC->DeleteDC(); 
		MemDC=NULL; 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CDEMView printing 
 
BOOL CDEMView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CDEMView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CDEMView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CDEMView diagnostics 
 
#ifdef _DEBUG 
void CDEMView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CDEMView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CDEMDoc* CDEMView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDEMDoc))); 
	return (CDEMDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CDEMView message handlers 
 
void CDEMView::MapOpen(DEMdata *demdata) 
{ 
	CDEMView::demdata=demdata; 
	mousepick=true; 
	SetCursor(AfxGetApp()->LoadCursor(IDC_PICK)); 
 
	CRect clientrect; 
	GetClientRect(&clientrect); 
	mapview.ReFlesh(clientrect,demdata->GetSizeX(),demdata->GetSizeY()); 
	demdata->SetH(); 
	demdata->matchcolor(); 
	drawlengend=true; 
	draw_3D=false; 
 
	CDC* pDC=GetDC(); 
	CRect rect; 
	GetClientRect(&rect); 
	if(CDEMView::maploadok) 
	{ 
		MemBitmap.DeleteObject(); 
		ReleaseDC(MemDC); 
	} 
	MemDC=new CDC; 
	MemDC->CreateCompatibleDC(pDC); 
	MemBitmap.CreateCompatibleBitmap(pDC,demdata->GetSizeX(),demdata->GetSizeY()); 
	MemDC->SelectObject(&MemBitmap); 
 
	ReleaseDC(pDC); 
 
	UpDataDemDraw(); 
	maploadok=true; 
	moving=false; 
	Invalidate(); 
} 
 
void CDEMView::ReFlesh() 
{	//显示比例归位(100%) 
	CRect clientrect; 
	GetClientRect(&clientrect); 
    mapview.ReFlesh(clientrect,demdata->GetSizeX(),demdata->GetSizeY()); 
	viewport.ReFlesh(); 
	Invalidate(); 
} 
 
 
void CDEMView::OnUpdateReflesh(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->Enable(maploadok);	 
} 
 
void CDEMView::ZoomIn() 
{ 
	mapview.ZoomIn(); 
	Invalidate(); 
} 
 
 
void CDEMView::OnUpdateZoomin(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->Enable(maploadok);	 
} 
 
 
void CDEMView::ZoomOut() 
{ 
	mapview.ZoomOut(); 
	Invalidate(); 
} 
 
 
void CDEMView::OnUpdateZoomout(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->Enable(maploadok);	 
} 
 
void CDEMView::OnClickEnlarge()  
{ 
	// TODO: Add your command handler code here 
	clickenlarge=true; 
	clickdrag=false; 
	mousepick=false; 
 
	SetOperateStatusBar("比例"); 
	SetCursor(AfxGetApp()->LoadCursor(IDC_ZOOMIN)); 
} 
 
void CDEMView::OnUpdateClickEnlarge(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(clickenlarge); 
	pCmdUI->Enable(maploadok); 
} 
 
void CDEMView::OnMouseDrag()  
{ 
	// TODO: Add your command handler code here 
	clickdrag=true; 
	clickenlarge=false; 
	mousepick=false; 
	SetOperateStatusBar("平移"); 
	SetCursor(AfxGetApp()->LoadCursor(IDC_HAND)); 
} 
 
void CDEMView::OnUpdateMouseDrag(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(clickdrag); 
	pCmdUI->Enable(maploadok); 
} 
 
void CDEMView::OnPick()  
{ 
	// TODO: Add your command handler code here 
	mousepick=true; 
	clickdrag=false; 
	clickenlarge=false; 
	SetOperateStatusBar("选取"); 
	SetCursor(AfxGetApp()->LoadCursor(IDC_PICK)); 
} 
 
void CDEMView::OnUpdatePick(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->Enable(maploadok); 
	pCmdUI->SetCheck(mousepick); 
} 
 
BOOL CDEMView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(maploadok)return true; 
	else  SetCursor(AfxGetApp()->LoadCursor(IDC_ARROW)); 
	// return CGLEnabledView::OnSetCursor(pWnd, nHitTest, message);	 
	return true; 
} 
 
void CDEMView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	 
	SetCapture(); 
	if(mousepick)oldpoint=point; 
//地图整体平移 
	if(clickdrag) 
	{    
		mousedrag=true; 
		oldpoint=point; 
 
		firstdrag=true; 
		SetCursor(AfxGetApp()->LoadCursor(IDC_HANDCLICK)); 
	} 
//点击放大 
	if(clickenlarge) 
	{	 
		buttondown=true; 
		enlarge=true; 
 
		firstdraw=true; 
		mapview.SetCurrentPoint(point); 
		SetCursor(AfxGetApp()->LoadCursor(IDC_ZOOMIN)); 
	} 
	mapview.SetOldPoint(point);	 
	CView::OnLButtonDown(nFlags, point); 
} 
 
void CDEMView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	CMainFrame *pFrm=(CMainFrame*)AfxGetApp()->m_pMainWnd; 
	CMDIChildWnd *pChild = pFrm->MDIGetActive(); 
 
	if(!(this==(CDEMView*)pChild->GetActiveView()))return; 
	// TODO: Add your message handler code here and/or call default 
	if(mousepick&&GetCapture()==this&&!maploadok) 
	{ 
		El+=double(point.x-oldpoint.x)/3.6; 
		Az+=double(point.y-oldpoint.y)/3.6; 
 
		if(Az<-180)Az+=360;  if(El<-180)El+=360; 
		if(Az>180)Az-=360;  if(El>180)El-=360; 
	// 更新场景 
		oldpoint=point; 
		Invalidate(TRUE); 
	} 
	if(mousedrag) 
	{//整体平移 
	mapview.MoveWithMouse(point); 
	Invalidate(); 
	} 
	if(buttondown) 
	{  
    CDC *pDC=GetDC(); 
 
	mapview.DrawCret(point,pDC,firstdraw); 
 
	ReleaseDC(pDC); 
	firstdraw=false; 
	enlarge=false; 
	} 
	//改变鼠标形状----点击放大 
	if(mousepick) 
	{ 
		SetOperateStatusBar("选取"); 
        SetCursor(AfxGetApp()->LoadCursor(IDC_PICK)); 
	} 
    if(clickenlarge) 
	{ 
		SetOperateStatusBar("比例"); 
		SetCursor(AfxGetApp()->LoadCursor(IDC_ZOOMIN)); 
	} 
    if(clickdrag) 
	{	//点击选取平移 
		if(mousedrag)SetCursor(AfxGetApp()->LoadCursor(IDC_HANDCLICK)); 
		else SetCursor(AfxGetApp()->LoadCursor(IDC_HAND)); 
		SetOperateStatusBar("平移"); 
	} 
	CStatusBar *pStatus=&pFrm->m_wndStatusBar; 
	if(pStatus) 
	{	//写状态栏的设备坐标 
		CString strMouse; 
		strMouse.Format("x=%3d  y=%3d",point.x,point.y); 
		pStatus->SetPaneText(pStatus->CommandToIndex(ID_INDICATOR_MOUSE),strMouse); 
	} 
	if(pStatus) 
	{	//写状态栏大地坐标 
		if(maploadok&!draw_3D) 
		{ 
		CString strMouse; 
 
		strMouse.Format("x=%6.3f  y=%6.3f",demdata->X0+point.x*demdata->DX,demdata->Y0+point.y*demdata->DY); 
		pStatus->SetPaneText(pStatus->CommandToIndex(ID_INDICATOR_GISCOOR),strMouse); 
 
		pointattrib.UpDataPoint(point); 
		pFrm->pointattrib.UpdataPointAttrib(pointattrib,viewport.Az,viewport.El,viewport,demdata); 
		} 
	} 
	if(maploadok&draw_3D) 
	{ 
	CDEMView::point=point; 
	Updata3DCoor(point); 
	pFrm->pointattrib.UpdataPointAttrib(pointattrib,viewport.Az,viewport.El,viewport,demdata); 
	} 
	CView::OnMouseMove(nFlags, point); 
} 
void CDEMView::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	 
if(clickdrag)SetCursor(AfxGetApp()->LoadCursor(IDC_HAND)); 
if(mousedrag) 
	{//整体平移 
	mapview.MoveWithMouse(point); 
	Invalidate(); 
	} 
if(clickenlarge&enlarge) 
	{//点击放大弹起后放大重绘 
	mapview.WindowMove(point); 
	mapview.ZoomIn(); 
	Invalidate(); 
	} 
if(buttondown&!enlarge) 
	{//点击拉框放大操作重绘 
	mapview.ShowRect(point); 
	Invalidate(); 
	buttondown=false;	 
	} 
if(setflylist) 
	{ 
	FlyList *flylist=new FlyList; 
	flylist->newFlyList(fx,fy); 
	viewport.setflylist.AddTail(flylist); 
	Invalidate(); 
	} 
    mousedrag=false; 
	buttondown=false; 
	ReleaseCapture(); 
	CView::OnLButtonUp(nFlags, point); 
} 
 
void CDEMView::OnRButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(clickenlarge)SetCursor(AfxGetApp()->LoadCursor(IDC_ZOOMOUT)); 
	 
	CView::OnRButtonDown(nFlags, point); 
} 
 
void CDEMView::OnRButtonUp(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(maploadok) 
	{ 
		mapview.ZoomOut(); 
		Invalidate(); 
	} 
	if(clickenlarge)SetCursor(AfxGetApp()->LoadCursor(IDC_ZOOMIN)); 
 
	CView::OnRButtonUp(nFlags, point);	 
} 
 
 
BOOL CDEMView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)  
{ 
	// TODO: Add your message handler code here and/or call default 
 
	if(!draw_3D)mapview.WheelZoom(zDelta/120,pt); 
	else 
	{ 
		viewport.MoveHigh(-zDelta/120); 
		viewport.MoveForward(zDelta/120); 
//		viewport.SetEl(); 
	} 
	Invalidate();	 
	return CView::OnMouseWheel(nFlags, zDelta, pt); 
} 
 
void CDEMView::OnUpdateShowscale(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
		pCmdUI->Enable(maploadok); 
} 
 
void CDEMView::OnUpdateMapColor(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
		pCmdUI->Enable(maploadok); 
} 
 
MapViewAttrib & CDEMView::Getmapview() 
{ 
return mapview; 
} 
 
DEMdata* CDEMView::GetDEMdata() 
{ 
return demdata; 
} 
 
void CDEMView::SetOperateStatusBar(char *index) 
{ 
	CMainFrame * pFrm=(CMainFrame *)AfxGetApp()->m_pMainWnd; 
	CStatusBar * pStatus=&pFrm->m_wndStatusBar; 
	if(pStatus)	 
	{	 
		pStatus->SetPaneText(pStatus->CommandToIndex(ID_INDICATOR_OPERATE),index);	 
	} 
} 
 
 
void CDEMView::OnSize(UINT nType, int cx, int cy)  
{ 
	CGLEnabledView::OnSize(nType, cx, cy);	 
	// TODO: Add your message handler code here 
	SetTimer(0,30,NULL); 
	CRect clientrect(0,0,cx,cy); 
	CPoint rect(clientrect.Width(),clientrect.Height()); 
	if(maploadok)mapview.ViewportChange(clientrect,demdata->GetSizeX(),demdata->GetSizeY()); 
} 
 
void CDEMView::DrawLengend() 
{ 
	CMainFrame * pFrm=(CMainFrame *)AfxGetApp()->m_pMainWnd; 
	CClientDC dc(&(pFrm->showtoolbar)); 
	int middle=0; 
	CRect rect; 
	pFrm->showtoolbar.GetWindowRect(rect); 
	rect.right-=rect.left;rect.bottom-=rect.top; 
	rect.left=0;rect.top=0; 
	middle=rect.right/2-60; 
	dc.MoveTo(middle+19,49);	dc.LineTo(middle+19,rect.bottom-31); 
	dc.MoveTo(middle+71,49);	dc.LineTo(middle+71,rect.bottom-31); 
	dc.MoveTo(middle+19,49);	dc.LineTo(middle+71,49); 
	dc.MoveTo(middle+19,rect.bottom-31);	dc.LineTo(middle+71,rect.bottom-31); 
	for(int i=50;i<=rect.bottom-32;i++) 
	{ 
		CPen pen(PS_SOLID,1,demdata->GetLengendColor((rect.bottom-32-i)*1020/(rect.bottom-31-50))); 
		dc.SelectObject(&pen); 
		dc.MoveTo(middle+20,i); 
		dc.LineTo(middle+71,i); 
	} 
	int RealSizeH=demdata->SizeH/demdata->Hzoom; 
	int lengendH=RealSizeH;int n=1; 
	while(lengendH/10) 
	{ 
		lengendH/=10; 
		n*=10; 
	} 
	dc.SetBkColor(RGB(239,235,222)); 
	CPen pen(PS_SOLID,1,RGB(0,0,0)); 
	dc.SelectObject(&pen); 
	for(i=0;i<=lengendH;i++) 
	{ 
		int j=rect.bottom-31-n*i*(rect.bottom-31-50)/RealSizeH; 
		dc.MoveTo(middle+20,j);dc.LineTo(middle+23,j); 
		dc.MoveTo(middle+65,j);dc.LineTo(middle+70,j); 
		CString string; 
		string.Format("%d",n*i); 
		dc.TextOut(middle+75,j-8,string); 
	} 
	dc.TextOut(middle+30,15,"图例"); 
	dc.TextOut(middle+80,30,"(米)"); 
	ReleaseDC(&dc); 
 
	CClientDC dc1(&pFrm->drawtoolbar); 
	CRect rect1;	pFrm->drawtoolbar.GetItemRect(pFrm->drawtoolbar.CommandToIndex(ID_COLOR1),&rect1); 
	CBrush brush1(demdata->cr1);    dc1.FillRect(rect1,&brush1);  
 
	CRect rect2;	pFrm->drawtoolbar.GetItemRect(pFrm->drawtoolbar.CommandToIndex(ID_COLOR2),&rect2); 
	CBrush brush2(demdata->cr2);    dc1.FillRect(rect2,&brush2); 
 
	dc1.TextOut(rect1.left+8,7," S "); 
	dc1.TextOut(rect2.left+8,7," E "); 
	ReleaseDC(&dc1); 
} 
 
 
void CDEMView::OnShowframe()  
{ 
	// TODO: Add your command handler code here 
	showframe=!showframe; 
	Invalidate(); 
} 
 
void CDEMView::OnUpdateShowframe(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(showframe); 
} 
 
CRect CDEMView::GetMapRect() 
{ 
		CDC *pDC=GetDC(); 
 
		CRect clientrect;CPoint point; 
		GetClientRect(clientrect); 
 
		point.x=0;point.y=clientrect.bottom; 
		pDC->DPtoLP(&point); 
//		point=demdata->CPointTransXYCoor(point,mapview.GetShowScale(),mapview.GetWindowOrg()); 
		clientrect.left=point.x;clientrect.top=point.y; 
 
		point.x=clientrect.right;point.y=0; 
		pDC->DPtoLP(&point); 
//		point=demdata->CPointTransXYCoor(point,mapview.GetShowScale(),mapview.GetWindowOrg()); 
		clientrect.right=point.x;clientrect.bottom=point.y; 
		if(clientrect.left>clientrect.right) 
		{ 
			int temp=clientrect.left; 
			clientrect.left=clientrect.right; 
			clientrect.right=temp; 
		} 
		if(clientrect.top>clientrect.bottom) 
		{ 
			int temp=clientrect.top; 
			clientrect.top=clientrect.bottom; 
			clientrect.bottom=temp; 
		} 
		ReleaseDC(pDC); 
		return clientrect; 
} 
 
 
 
void CDEMView::OnColor1()  
{ 
	if(!maploadok)return; 
	// TODO: Add your command handler code here 
	CColorDialog color; 
	if(color.DoModal()==IDCANCEL)return; 
	demdata->cr1=color.GetColor(); 
 
	CMainFrame* pMFrm=(CMainFrame *)AfxGetMainWnd(); 
	pMFrm->drawtoolbar.mapcolorcombox.SetWindowText("gradualcolor"); 
	demdata->mapcolor="gradualcolor"; 
 
	UpDataDemDraw(); 
	Invalidate(); 
	DrawLengend(); 
	drawlengend=false; 
} 
 
void CDEMView::OnUpdateColor1(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
//	pCmdUI->Enable(maploadok); 
} 
 
void CDEMView::OnColor2()  
{ 
	if(!maploadok)return; 
	// TODO: Add your command handler code here 
	CColorDialog color; 
	if(color.DoModal()==IDCANCEL)return; 
	demdata->cr2=color.GetColor(); 
 
	CMainFrame* pMFrm=(CMainFrame *)AfxGetMainWnd(); 
	pMFrm->drawtoolbar.mapcolorcombox.SetWindowText("gradualcolor"); 
	demdata->mapcolor="gradualcolor"; 
 
	UpDataDemDraw(); 
	Invalidate(); 
	DrawLengend(); 
	drawlengend=false; 
} 
 
void CDEMView::OnUpdateColor2(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
//	pCmdUI->Enable(maploadok); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
//3D_OPenGL 
 
void CDEMView::OnDraw(CDC* pDC) 
{ 
	if(draw_3D)CGLEnabledView::OnDraw(pDC); 
 
	CDEMDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
	drawlengend=true; 
 
	if(maploadok&&!draw_3D) 
	{ 
	CDC MemScrDC; 
	CBitmap MemScrBitmap; 
	CRect rect; 
 
	GetClientRect(&rect); 
	MemScrDC.CreateCompatibleDC(pDC); 
	MemScrBitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height()); 
	MemScrDC.SelectObject(&MemScrBitmap); 
	MemScrDC.FillSolidRect(0,0,rect.Width(),rect.Height(),RGB(255,255,255)); 
	MemScrDC.StretchBlt( 
			mapview.GetWindowOrg().x,mapview.GetWindowOrg().y, 
			int(mapview.GetWindowExt().x*mapview.GetShowScale()),int(mapview.GetWindowExt().y*mapview.GetShowScale()), 
			MemDC,0,0,demdata->GetSizeX(),demdata->GetSizeY(),SRCCOPY); 
	if(showframe)mapview.DrawGrad(&MemScrDC,demdata->GetSizeX(),demdata->GetSizeY()); 
 
	pDC->BitBlt(0,0,rect.Width(),rect.Height(),&MemScrDC,0,0,SRCCOPY); 
 
	MemScrBitmap.DeleteObject();  
	MemScrDC.DeleteDC();  
	} 
} 
 
void CDEMView::OnDrawGL() 
{ 
	 
	CRect rect;GetClientRect(&rect); 
	if(!maploadok)Draw_3DHead(rect); 
	else 
	{ 
	glPushMatrix();	 
	glScalef(100,100,100); 
	gluLookAt(viewport.eyex/100,viewport.eyey/100,viewport.eyez/100, 
		viewport.centerx/100,viewport.centery/100,viewport.centerz/100,0,0,1); 
 
	glTranslated(viewport.eyex/100,viewport.eyey/100,0); 
	glRotated(viewport.Az,0,0,1.0); 
	glTranslated(-viewport.eyex/100,-viewport.eyey/100,0); 
 
 
//	glViewport(0,0,rect.Width(),rect.Height()); 
	if(!showframe)	glCallList(4); 
	else	glCallList(5); 
	if(showskirt) glCallList(6); 
	glCallList(1); 
	Drawtext(); 
	if(showpath)DrawPath(); 
	glPopMatrix(); 
	} 
} 
 
 
BOOL CDEMView::OnEraseBkgnd(CDC* pDC)  
{ 
	// TODO: Add your message handler code here and/or call default 
	return CGLEnabledView::OnEraseBkgnd(pDC); 
} 
 
 
 
void CDEMView::On2d()  
{ 
	// TODO: Add your command handler code here 
	draw_3D=false; 
	stop=true; 
	Invalidate(); 
} 
 
void CDEMView::OnUpdate2d(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(!draw_3D); 
 
} 
 
void CDEMView::On3d()  
{ 
	// TODO: Add your command handler code here 
	stop=false; 
	draw_3D=true; 
	Invalidate(); 
} 
 
void CDEMView::OnUpdate3d(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(draw_3D);	 
} 
 
 
void CDEMView::OnMoving()  
{ 
	// TODO: Add your command handler code here 
	moving=!moving; 
	stop=false; 
	autofly=false; 
} 
 
void CDEMView::OnUpdateMoving(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(moving); 
	pCmdUI->Enable(!setflylist); 
} 
 
void CDEMView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)  
{ 
	// TODO: Add your message handler code here and/or call default 
	KeyEvent(nChar); 
	CGLEnabledView::OnKeyDown(nChar, nRepCnt, nFlags); 
} 
 
void CDEMView::KeyEvent(UINT nChar) 
{ 
	CMainFrame *pFrm=(CMainFrame*)AfxGetApp()->m_pMainWnd; 
	if(nChar=='M')moving=!moving; 
	viewport.ProcessKeyEvent(nChar,autofly); 
	Updata3DCoor(point); 
	pFrm->pointattrib.UpdataPointAttrib(pointattrib,viewport.Az,viewport.El,viewport,demdata); 
	Invalidate(); 
} 
 
 
void CDEMView::Drawtext() 
{ 
	glColor3f(1,0,0); 
	COpenGLText text1("x",0.05,"楷体_GB2312", 
	    demdata->s*demdata->SizeX*demdata->DX+demdata->arrow_dis,0,0); 
 
	glColor3f(0,1,0); 
	COpenGLText text2("y",0.05,"楷体_GB2312", 
		0,demdata->s*demdata->SizeY*demdata->DY+demdata->arrow_dis,0); 
 
	glColor3f(0,0,1); 
	COpenGLText text3("z",0.05,"楷体_GB2312", 
		0,0,demdata->SizeH/demdata->Hzoom*demdata->s+demdata->arrow_dis,90,1,0,0); 
} 
 
void CDEMView::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(stop)return; 
 
	CMainFrame *pFrm=(CMainFrame*)AfxGetApp()->m_pMainWnd; 
	CMDIChildWnd *pChild = pFrm->MDIGetActive(); 
 
	if(!(this==(CDEMView*)pChild->GetActiveView()))return; 
	if(!maploadok) 
	{ 
		Az -= 3;	El -= 2;   
		if(Az<-180)Az+=360;  if(El<-180)El+=360; 
		if(Az>180)Az-=360;  if(El>180)El-=360; 
		earthrotate+=3;	moonrotate+=0.6; bothrotate+=0.5; 
		pFrm->pointattrib.UpdataPointAttrib(pointattrib,Az,El,viewport,false); 
		InvalidateRect(NULL,FALSE); 
	} 
	else 
	{ 
		if(moving)viewport.MoveForward(1,viewport.speed,viewport.speed); 
		if(autofly)viewport.AutoFly(); 
		if(moving|autofly) 
		{ 
		InvalidateRect(NULL,FALSE); 
		Updata3DCoor(point); 
		pFrm->pointattrib.UpdataPointAttrib(pointattrib,viewport.Az,viewport.El,viewport,demdata); 
		} 
	} 
	CGLEnabledView::OnTimer(nIDEvent); 
} 
 
void CDEMView::Draw_3DHead(CRect rect) 
{ 
	glPushMatrix(); 
	glRotated(Az,0.0,0.0,1.0); 
	glRotated(El,1.0,-1.0,0.0); 
	glScaled(0.4,0.4,0.4);  
	glViewport(-rect.Width()/4,rect.Height()/4.5,rect.Width(),rect.Height()); 
	glCallList(2); 
	glPopMatrix(); 
 
	glPushMatrix(); 
	glRotated(30,1.0,0.0,0.0); 
 	GLfloat position[]={0.0,(float)1.8,-5.0,1.0}; 
	glLightfv(GL_LIGHT0,GL_POSITION,position); 
	glRotated(El,0.0,1.0,0.0); 
	glScaled(0.8,0.8,0.8); 
	glViewport(+rect.Width()/4,rect.Height()/4.5,rect.Width(),rect.Height()); 
	glEnable(GL_LIGHTING); 
	glCallList(3); 
	glDisable(GL_LIGHTING); 
	glPopMatrix(); 
 
	glEnable(GL_TEXTURE_2D); 
	glViewport(0,rect.Height()/4.5,rect.Width(),rect.Height()); 
 
	glPushMatrix();	 
	glRotated(23.5,1.0,0.0,0.0); 
	glPushMatrix(); 
	glRotated(bothrotate,0,1,0); 
	glTranslated(2,0,0); 
	glRotated(90,-1,0,0); 
	glRotated(moonrotate,0.0,0.0,1.0); 
	glScaled(0.2,0.2,0.2);  
	glCallList(8); 
	glPopMatrix(); 
 
	glRotated(90,-1,0,0); 
	glRotated(earthrotate,0.0,0.0,1.0); 
	glScaled(1,1,1);  
	glCallList(7); 
 
	glPushMatrix(); 
	glScaled(2,2,2); 
	glCallList(9); 
	glPopMatrix(); 
	glPopMatrix(); 
	glDisable(GL_TEXTURE_2D); 
 
	glPushMatrix(); 
	glViewport(0,0,rect.Width(),rect.Height()); 
	glColor3f(0,0,1); 
	COpenGLText text1("DEM(二维)三维地形漫游系统",0.5,"楷体_GB2312",-3.5,-0.8,0,Az,0,1,0); 
	glColor3f(1,0,0); 
	COpenGLText text2("程式设计:宋启波",0.5,"楷体_GB2312",-2,-2,0); 
	glColor3f(0,0,0); 
	COpenGLText text3("(三维操作参见帮助)",0.4,"楷体_GB2312",-2,-3,0); 
	glPopMatrix(); 
} 
 
void CDEMView::UpDataDemDraw() 
{ 
	MemDC->FillSolidRect(0,0,demdata->GetSizeX(),demdata->GetSizeY(),RGB(255,255,0)); 
	demdata->matchcolor(); 
	demdata->DrawDEM(MemDC); 
 
	wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC); 
	glDeleteLists(1,1); 
	glDeleteLists(4,3); 
	 
	glTranslated(10,10,0); 
	viewport.eyex+=10;viewport.eyey+=10; 
	viewport.centerx+=10;viewport.centery+=10; 
	demdata->DrawAxis(); 
	demdata->Draw3DSurf(); 
	demdata->Draw3DMesh(); 
	demdata->Draw3DSkirt(); 
	wglMakeCurrent(NULL,NULL); 
} 
 
void CDEMView::Updata3DCoor(CPoint point) 
{ 
if(maploadok&draw_3D) 
	{ 
	GLdouble modelMatrix[16]; 
	GLdouble projMatrix[16]; 
	GLint glviewport[4]; 
	CRect rect;GetClientRect(&rect); 
 
	wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC); 
	glPushMatrix(); 
	gluLookAt(viewport.eyex,viewport.eyey,viewport.eyez, 
		viewport.centerx,viewport.centery,viewport.centerz,0,0,1); 
 
	glTranslated(viewport.eyex,viewport.eyey,0); 
	glRotated(viewport.Az,0,0,1.0); 
	glTranslated(-viewport.eyex,-viewport.eyey,0); 
 
	glScalef(100.0,100.0,100.0); 
	glViewport(0,0,rect.Width(),rect.Height()); 
 
	glGetDoublev(GL_MODELVIEW_MATRIX  ,modelMatrix);  
	glGetDoublev(GL_PROJECTION_MATRIX ,projMatrix);  
	glGetIntegerv(GL_VIEWPORT,glviewport);  
 
	int X=point.x; 
	int Y=glviewport[3]-point.y; 
 
	float Z; 
	glReadBuffer(GL_BACK); 
	glReadPixels(X,Y,1,1,GL_DEPTH_COMPONENT ,GL_FLOAT, &Z); 
	GLdouble fz; 
	gluUnProject(X,Y,Z,modelMatrix,projMatrix,glviewport,&fx,&fy,&fz); 
		 
	glPopMatrix(); 
	wglMakeCurrent(NULL,NULL); 
	CPoint datapoint; 
	datapoint.x=(int)(fx*demdata->Com_3D); 
	datapoint.y=(int)(fy*demdata->Com_3D); 
	pointattrib.UpDataPoint(datapoint); 
	}		 
} 
 
void CDEMView::OnAutofly()  
{ 
	// TODO: Add your command handler code here 
	autofly=!autofly; 
	moving=false; 
	stop=false; 
	viewport.ReFlesh(); 
	viewport.SetViewPort(0); 
} 
 
void CDEMView::OnUpdateAutofly(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(autofly); 
	pCmdUI->Enable(!setflylist); 
} 
 
void CDEMView::OnAutoheight()  
{ 
	// TODO: Add your command handler code here 
	viewport.autoeyez=!viewport.autoeyez; 
	viewport.eyez=10; 
} 
 
void CDEMView::OnUpdateAutoheight(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(viewport.autoeyez); 
	pCmdUI->Enable(!setflylist); 
} 
 
void CDEMView::OnAppAbout()  
{ 
	// TODO: Add your command handler code here 
	stop=true; 
	CAboutDlg aboutDlg; 
	aboutDlg.DoModal();	 
	stop=false; 
} 
 
void CDEMView::OnStop()  
{ 
	// TODO: Add your command handler code here 
	stop=!stop; 
} 
 
void CDEMView::OnUpdateStop(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(stop); 
	pCmdUI->Enable(!setflylist); 
	if(inittoolbar) 
	{ 
		stop=false; 
		inittoolbar=false; 
	} 
} 
 
void CDEMView::OnKeyw()  
{ 
	// TODO: Add your command handler code here 
		KeyEvent('W'); 
} 
void CDEMView::OnKeys()  
{ 
	// TODO: Add your command handler code here 
		KeyEvent('S'); 
} 
 
void CDEMView::OnKeya()  
{ 
	// TODO: Add your command handler code here 
		KeyEvent('A'); 
} 
 
void CDEMView::OnKeyd()  
{ 
	// TODO: Add your command handler code here 
		KeyEvent('D'); 
} 
 
void CDEMView::OnKeyq()  
{ 
	// TODO: Add your command handler code here 
		KeyEvent('Q'); 
} 
 
void CDEMView::OnKeye()  
{ 
	// TODO: Add your command handler code here 
		KeyEvent('E'); 
} 
 
void CDEMView::OnKeyf()  
{ 
	// TODO: Add your command handler code here 
		KeyEvent('F'); 
} 
 
void CDEMView::OnKeyr()  
{ 
	// TODO: Add your command handler code here 
		KeyEvent('R'); 
} 
 
void CDEMView::OnKeyadd()  
{ 
	// TODO: Add your command handler code here 
		KeyEvent(187); 
} 
 
void CDEMView::OnKeyreduse()  
{ 
	// TODO: Add your command handler code here 
		KeyEvent(189); 
} 
 
void CDEMView::OnKeyup()  
{ 
	// TODO: Add your command handler code here 
		KeyEvent(38); 
} 
 
void CDEMView::OnKeydown()  
{ 
	// TODO: Add your command handler code here 
		KeyEvent(40); 
} 
 
void CDEMView::OnKeyc()  
{ 
	// TODO: Add your command handler code here 
		viewport.MoveHigh(-1); 
		viewport.MoveForward(1); 
		viewport.SetEl(); 
		Invalidate(); 
} 
 
void CDEMView::OnKeyx()  
{ 
	// TODO: Add your command handler code here 
		viewport.MoveHigh(1); 
		viewport.MoveForward(-1); 
		viewport.SetEl(); 
		Invalidate(); 
} 
 
 
void CDEMView::OnSmooth()  
{ 
	// TODO: Add your command handler code here 
	viewport.smooth=!viewport.smooth; 
	viewport.flyeyenumber=1; 
	viewport.SetViewPort(0); 
	Invalidate(); 
} 
 
void CDEMView::OnUpdateSmooth(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(viewport.smooth); 
	pCmdUI->Enable(!setflylist); 
} 
 
void CDEMView::OnSetflylist()  
{ 
	// TODO: Add your command handler code here 
	if(!draw_3D)return; 
	if(!setflylist) 
	{ 
		stop=true; 
		setflylist=true; 
	} 
	else  
	{ 
		setflylist=false; 
		if(viewport.SetFlyList())AfxMessageBox("路线设置完成!", MB_OK); 
		else AfxMessageBox("控制点太少,路线设置失败!", MB_OK); 
		autofly=true; 
		moving=false; 
		stop=false; 
		viewport.ReFlesh(); 
		viewport.SetViewPort(0); 
	} 
} 
 
void CDEMView::OnUpdateSetflylist(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->Enable(maploadok); 
	pCmdUI->SetCheck(setflylist); 
} 
 
void CDEMView::OnShowpath()  
{ 
	// TODO: Add your command handler code here 
	showpath=!showpath;	 
	Invalidate(); 
} 
 
void CDEMView::OnUpdateShowpath(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetCheck(showpath); 
} 
 
void CDEMView::DrawPath() 
{ 
	glPushMatrix(); 
	glShadeModel(GL_SMOOTH); 
	glBegin(GL_LINES); 
	glColor3d(1.0,0.0,0.0); 
	if(setflylist) 
	{ 
	int n=viewport.setflylist.GetCount(); 
	if(n>1) 
	{ 
	POSITION pos=viewport.setflylist.GetHeadPosition(); 
	FlyList oldlist=*viewport.setflylist.GetAt(pos); 
	for(int i=0;i<n;i++) 
		{ 
		FlyList *list=viewport.setflylist.GetNext(pos); 
		glVertex3d(oldlist.nexteyex,oldlist.nexteyey,0.1); 
		glVertex3d(list->nexteyex,list->nexteyey,0.1); 
		oldlist=*list; 
		} 
	} 
	} 
	else if(viewport.smooth) 
	{ 
	for(int i=0;i<viewport.smoothlistnumber-1;i++) 
		 { 
		glVertex3d(viewport.smoothflylist[i].nexteyex,viewport.smoothflylist[i].nexteyey,0.1); 
		glVertex3d(viewport.smoothflylist[i+1].nexteyex,viewport.smoothflylist[i+1].nexteyey,0.1); 
		 } 
	} 
	else  
	{ 
	for(int i=0;i<viewport.listnumber-1;i++) 
		 { 
		glVertex3d(viewport.flylist[i].nexteyex,viewport.flylist[i].nexteyey,0.1); 
		glVertex3d(viewport.flylist[i+1].nexteyex,viewport.flylist[i+1].nexteyey,0.1); 
		 } 
	} 
	glEnd(); 
	glPopMatrix(); 
}