www.pudn.com > GpsSimulation.rar > RyxGpsView.cpp


// RyxGpsView.cpp : implementation of the CRyxGpsView class 
// 
 
#include "stdafx.h" 
#include "RyxGps.h" 
#include "MainFrm.h" 
#include "RyxGpsDoc.h" 
#include "RyxGpsView.h" 
#include  
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
//---------------------------------------------------------------------------------- 
#define ID_TIMER	1 
#define PI			3.1416 
///////////////////////////////////////////////////////////////////////////// 
// CRyxGpsView 
 
IMPLEMENT_DYNCREATE(CRyxGpsView, CView) 
 
BEGIN_MESSAGE_MAP(CRyxGpsView, CView) 
	//{{AFX_MSG_MAP(CRyxGpsView) 
	ON_COMMAND(ID_START, OnStart) 
	ON_UPDATE_COMMAND_UI(ID_START, OnUpdateStart) 
	ON_COMMAND(ID_END, OnEnd) 
	ON_UPDATE_COMMAND_UI(ID_END, OnUpdateEnd) 
	ON_WM_TIMER() 
	ON_WM_DESTROY() 
	ON_WM_ERASEBKGND() 
	ON_WM_MOUSEMOVE() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CRyxGpsView construction/destruction 
 
CRyxGpsView::CRyxGpsView() 
{ 
	// TODO: add construction code here 
	m_BkBrush.CreateSolidBrush(RGB(0,0,200)); 
	m_penWhite.CreatePen(PS_SOLID,0,RGB(255,255,255)); 
	m_penYellow.CreatePen(PS_SOLID,0,RGB(255,255,0)); 
	m_penRed.CreatePen(PS_SOLID,0,RGB(255,0,0)); 
	m_NullBrush.CreateStockObject(NULL_BRUSH); 
} 
 
CRyxGpsView::~CRyxGpsView() 
{ 
} 
 
BOOL CRyxGpsView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CRyxGpsView drawing 
 
void CRyxGpsView::OnDraw(CDC* pDC) 
{ 
	char pszNum[10]; 
	char pszTemp[50]; 
	int  iNum; 
	int  iOffset; 
	int  i; 
	int  alpha,sita; 
	int  iIndex; 
	CRyxGpsDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	pDoc->GetSubWord(22,pszNum); 
	iNum=atoi(pszNum); 
	if(pDoc->m_ReveiverID==1) 
		iOffset=22; 
	else if(pDoc->m_ReveiverID==2) 
	{ 
		switch(iNum) 
		{ 
		case 10: 
			iOffset=84; 
			break; 
		case 11: 
			iOffset=90; 
			break; 
		default: 
			break; 
		} 
	} 
	DrawAxis(pDC); 
	for(i=0;iGetSubWord(iOffset+i*6+1,pszTemp); 
		iIndex=atoi(pszTemp); 
		pDoc->GetSubWord(iOffset+i*6+2,pszTemp); 
		alpha=atoi(pszTemp);; 
		pDoc->GetSubWord(iOffset+i*6+3,pszTemp); 
		sita=atoi(pszTemp); 
		DrawPlanet(iIndex,alpha,sita,pDC); 
	} 
 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CRyxGpsView diagnostics 
 
#ifdef _DEBUG 
void CRyxGpsView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CRyxGpsView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CRyxGpsDoc* CRyxGpsView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRyxGpsDoc))); 
	return (CRyxGpsDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CRyxGpsView message handlers 
 
void CRyxGpsView::OnStart()  
{ 
	// TODO: Add your command handler code here 
	if(!m_bRunning) 
	{ 
		char s[256]; 
		CString str; 
		::GetCurrentDirectory(sizeof(s),s); 
		str=s; 
		if(str.GetAt(str.GetLength()-1)!='\\') 
			str+='\\'; 
		str+="Atti.txt"; 
		if((m_pFile=fopen(str,"rb"))==NULL) 
		{ 
			MessageBox("Can't open File"); 
			return; 
		} 
		else 
		{ 
			m_bRunning=TRUE; 
			SetTimer(ID_TIMER,100,NULL); 
		} 
	} 
} 
void CRyxGpsView::OnUpdateStart(CCmdUI* pCmdUI)  
{ 
	pCmdUI->Enable(!m_bRunning); 
} 
void CRyxGpsView::OnEnd()  
{ 
	if(m_bRunning) 
	{ 
		m_bRunning=FALSE; 
		KillTimer(ID_TIMER); 
		fclose(m_pFile); 
	} 
} 
void CRyxGpsView::OnUpdateEnd(CCmdUI* pCmdUI)  
{ 
	pCmdUI->Enable(m_bRunning); 
} 
void CRyxGpsView::OnInitialUpdate()  
{ 
	CView::OnInitialUpdate(); 
	m_bRunning=FALSE; 
} 
 
void CRyxGpsView::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CRyxGpsDoc* pDoc = GetDocument(); 
	char chPrevious; 
	char chCurrent; 
	int  i=0; 
	chPrevious=0; 
	while(i<3000) 
	{ 
		chCurrent=fgetc(m_pFile); 
		if(feof(m_pFile)) 
			OnEnd(); 
		if((chCurrent==0x0d)&&(chPrevious==0x0a)) //find 2 CR ('\n') 
			break; 
		pDoc->m_buffer[i]=chCurrent; 
		i++; 
		chPrevious=chCurrent; 
	} 
	pDoc->m_buffer[i]='\0'; 
	Invalidate();								//notify to update 
	POSITION pos = pDoc->GetFirstViewPosition(); 
	while (pos != NULL) 
	{ 
		CView* pView = pDoc->GetNextView(pos); 
		if(pView!=this) 
			pView->SendMessage(WM_MYNOTIFY,0,0); 
	}    
	CView::OnTimer(nIDEvent); 
} 
 
void CRyxGpsView::OnDestroy()  
{ 
	CView::OnDestroy(); 
	if(m_bRunning) 
	{ 
		m_bRunning=FALSE; 
		KillTimer(ID_TIMER); 
		fclose(m_pFile); 
	} 
} 
 
BOOL CRyxGpsView::OnEraseBkgnd(CDC* pDC)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CRect rect; 
	GetClientRect(&rect); 
	pDC->FillRect(&rect,&m_BkBrush); 
	return TRUE; 
} 
 
void CRyxGpsView::DrawAxis(CDC *pDC) 
{ 
	CRect rect; 
	int iWidth; 
	char strNum[10]; 
	CRyxGpsDoc* pDoc = GetDocument(); 
	pDoc->GetSubWord(22,strNum); 
	GetClientRect(&rect); 
	iWidth=min(rect.Width(),rect.Height()); 
	rect.right=rect.bottom=iWidth; 
	rect.InflateRect(-2,-2); 
	pDC->SetBkMode(TRANSPARENT); 
	pDC->SetTextColor(RGB(0,255,0)); 
	pDC->SelectObject(&m_penWhite); 
	pDC->MoveTo(rect.left,(rect.top+rect.bottom)/2); 
	pDC->LineTo(rect.right,(rect.top+rect.bottom)/2); 
	pDC->MoveTo((rect.left+rect.right)/2,rect.top); 
	pDC->LineTo((rect.left+rect.right)/2,rect.bottom); 
	rect.InflateRect(-30,-30); 
	pDC->TextOut(rect.right+20,(rect.top+rect.bottom)/2,CString("0")); 
	pDC->TextOut(rect.right+20,(rect.top+rect.bottom)/2-20,CString("90")); 
	pDC->TextOut((rect.right+rect.left)/2,rect.top-20,CString("0")); 
	pDC->TextOut((rect.right+rect.left)/2-30,rect.top-20,CString("360")); 
	pDC->TextOut(rect.left-30,(rect.top+rect.bottom)/2-20,CString("270")); 
	pDC->TextOut((rect.right+rect.left)/2,rect.bottom,CString("180")); 
	pDC->TextOut((rect.right+rect.left)/2+20,rect.top-20,CString("卫星天空视图")); 
	pDC->TextOut((rect.right+rect.left)/2+50,rect.bottom,CString("可用卫星数目: ")+strNum); 
	pDC->SelectObject(&m_NullBrush); 
	pDC->SelectObject(&m_penYellow); 
	pDC->Ellipse(&rect); 
	int delta; 
	delta=(int)(0.5*(double)rect.Width()*(1.0-cos(PI/6))); 
	pDC->Ellipse(rect.left+delta,rect.top+delta,rect.right-delta,rect.bottom-delta); 
	pDC->TextOut(rect.right-delta,(rect.top+rect.bottom)/2,CString("30"));; 
	rect.InflateRect(-rect.Width()/4,-rect.Height()/4); 
	pDC->Ellipse(&rect); 
	pDC->TextOut(rect.right,(rect.top+rect.bottom)/2,CString("60")); 
	pDC->TextOut((rect.right+rect.left)/2,(rect.top+rect.bottom)/2,CString("90")); 
 
} 
 
 
POINT CRyxGpsView::ConvertToLogic(int alpah, int sita) 
{ 
	CRect rect; 
	POINT pt,pt_ret; 
	int iWidth; 
	double R,r; 
	GetClientRect(&rect); 
	iWidth=min(rect.right,rect.bottom); 
	rect.right=rect.bottom=iWidth; 
	rect.InflateRect(-32,-32); 
	R=rect.Width()/2.0; 
	r=R*cos(PI*(double)alpah/180.0); 
	pt.x=(int)(r*cos(PI*(double)sita/180)); 
	pt.y=(int)(r*sin(PI*(double)sita/180)); //变为导航直角坐标 
	iWidth=pt.x; 
	pt.x=pt.y; 
	pt.y=iWidth;							//变为笛卡儿直角坐标 
	pt_ret.x=pt.x+(rect.right+rect.left)/2; 
	pt_ret.y=(rect.bottom+rect.top)/2-pt.y;	//变为设备坐标系 
	return pt_ret; 
} 
 
void CRyxGpsView::DrawPlanet(int iIndex,int alpah, int sita,CDC *pDC) 
{ 
	POINT pt; 
	char s[3]; 
	wsprintf(s,"%d",iIndex); 
	pDC->SelectObject(&m_penRed); 
	pt=ConvertToLogic(alpah,sita); 
	pDC->Ellipse(pt.x-10,pt.y-10,pt.x+10,pt.y+10); 
	pDC->SetTextColor(RGB(0,255,255)); 
	pDC->TextOut(pt.x-7,pt.y-7,s,lstrlen(s)); 
} 
 
void CRyxGpsView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CString str,str1; 
	CRect rect; 
	CRgn   rgn; 
	double alpha,sita; 
	double cos_alpha; 
	double sin_sita,cos_sita; 
	double distance; 
	CPoint ptOrigin;  
	GetClientRect(&rect); 
	rect.right=rect.bottom=min(rect.right,rect.bottom); 
	rect.InflateRect(-32,-32); 
	ptOrigin.x=(rect.left+rect.right)/2; 
	ptOrigin.y=(rect.top+rect.bottom)/2; 
	rgn.CreateEllipticRgn(rect.left,rect.top,rect.right,rect.bottom); 
	CMainFrame *pFrame=(CMainFrame *)AfxGetMainWnd(); 
	if(rgn.PtInRegion(point)) 
	{ 
		distance=sqrt(pow(ptOrigin.x-point.x,2)+pow(ptOrigin.y-point.y,2)); 
		cos_alpha=distance/(rect.Width()/2.0); 
		alpha=acos(cos_alpha); 
		str.Format("高度角α=%.2f",alpha*180.0/PI); 
		sin_sita=(point.x-ptOrigin.x)/distance; 
		cos_sita=-(point.y-ptOrigin.y)/distance; 
		sita=acos(cos_sita); 
		if(sin_sita>=0) 
			sita=sita*180.0/PI; 
		else 
			sita=-sita*180.0/PI+360; 
		str1.Format(",方位角θ=%f",sita); 
		str+=str1; 
		pFrame->m_wndStatusBar.SetPaneText(2,str); 
	} 
	else 
		pFrame->m_wndStatusBar.SetPaneText(2,"工作正常!"); 
 
	CView::OnMouseMove(nFlags, point); 
}