www.pudn.com > Snakes.rar > SnakesView.cpp


// SnakesView.cpp : implementation of the CSnakesView class 
// 
 
#include "stdafx.h" 
#include "Snakes.h" 
 
#include "SnakesDoc.h" 
#include "SnakesView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CSnakesView 
 
IMPLEMENT_DYNCREATE(CSnakesView, CView) 
 
BEGIN_MESSAGE_MAP(CSnakesView, CView) 
	//{{AFX_MSG_MAP(CSnakesView) 
	ON_WM_LBUTTONUP() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_MOUSEMOVE() 
	ON_COMMAND(ID_START, OnStart) 
	ON_WM_TCARD() 
	ON_WM_TIMER() 
	ON_COMMAND(ID_TT, OnTt) 
	ON_COMMAND(ID_SS, OnSs) 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSnakesView construction/destruction 
 
CSnakesView::CSnakesView() 
{ 
	// TODO: add construction code here 
dd=false; 
} 
 
CSnakesView::~CSnakesView() 
{ 
} 
 
BOOL CSnakesView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CSnakesView drawing 
 
void CSnakesView::OnDraw(CDC* pDC) 
{ 
	CSnakesDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	BeginWaitCursor(); 
	 
	// 获取DIB 
	HDIB hDIB = pDoc->GetHDIB(); 
	 
	// 判断DIB是否为空 
	if (hDIB != NULL) 
	{ 
		LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB); 
		 
		// 获取DIB宽度 
		int cxDIB = (int) ::DIBWidth(lpDIB); 
		 
		// 获取DIB高度 
		int cyDIB = (int) ::DIBHeight(lpDIB); 
		 
		::GlobalUnlock((HGLOBAL) hDIB); 
		 
		CRect rcDIB; 
		rcDIB.top = rcDIB.left = 0; 
		rcDIB.right =cxDIB; 
		rcDIB.bottom =cyDIB; 
		 
		CRect rcDest; 
		 
	 
		// 非打印 
		{ 
			// 不必缩放图像 
			rcDest = rcDIB; 
			 
			 
			 
		} 
		 
		// 输出DIB 
		::PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(), 
			&rcDIB, pDoc->GetDocPalette()); 
	} 
	 
	// 恢复正常光标 
	EndWaitCursor(); 
	 
	// TODO: add draw code for native data here 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CSnakesView printing 
 
BOOL CSnakesView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CSnakesView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CSnakesView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CSnakesView diagnostics 
 
#ifdef _DEBUG 
void CSnakesView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CSnakesView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CSnakesDoc* CSnakesView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSnakesDoc))); 
	return (CSnakesDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CSnakesView message handlers 
 
void CSnakesView::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
    if(dd) 
	{ 
		CDC*pDC=GetDC(); 
	m_pSnake->LButtonUp(point,pDC); 
 
	} 
	 
	CView::OnLButtonUp(nFlags, point); 
} 
 
void CSnakesView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	 if(dd) 
	{ 
	CDC*pDC=GetDC(); 
	m_pSnake->LButtonDown(point,pDC); 
	 } 
	CView::OnLButtonDown(nFlags, point); 
} 
 
void CSnakesView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	 if(dd) 
	{ 
	CDC*pDC=GetDC(); 
	m_pSnake->MouseDownAndMove(point,pDC); 
	 } 
	CView::OnMouseMove(nFlags, point); 
	 
} 
 
void CSnakesView::OnStart()  
{ 
	// TODO: Add your command handler code here 
	CSnakesDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	HDIB hDIB=pDoc->m_hDIB; 
	// 由DIB句柄得到DIB指针并锁定DIB 
	CDC*pDC=GetDC(); 
	m_pSnake=new CSnakes(hDIB); 
	dd=true; 
	//m_pSnake->GVF(pDC); 
	//m_pSnake->DrawGV(pDC); 
 
} 
 
void CSnakesView::OnTCard(UINT idAction, DWORD dwActionData)  
{ 
	// TODO: Add your message handler code here 
	 
} 
 
void CSnakesView::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	m_pSnake->Snake_algorithm(); 
	 
float x,y; 
	CDC *pDC; 
	pDC=GetDC(); 
	RedrawWindow(); 
	CPen pen1(PS_SOLID,2,(COLORREF)255*100),pen2(PS_SOLID,1,(COLORREF)255*255); 
	CPen* pOldPen=pDC->SelectObject(&pen1);	 
	int i=0; 
	pDC->MoveTo(m_pSnake->Snake_points[0]); 
	for(i=0;ip_NumPos;i++) 
	{ 
		pDC->LineTo(m_pSnake->Snake_points[i]); 
		x=m_pSnake->Snake_points[i].x; 
		y=m_pSnake->Snake_points[i].y; 
		pOldPen=pDC->SelectObject(&pen2);	 
		pDC->Ellipse(x-2,y-2,x+2,y+2); 
	} 
	CView::OnTimer(nIDEvent); 
} 
 
void CSnakesView::OnTt()  
{ 
	// TODO: Add your command handler code here 
	 
	 
} 
 
void CSnakesView::OnSs()  
{ 
	// TODO: Add your command handler code here 
	SetTimer(1,200,NULL); 
}