www.gusucode.com > VC画线源码,画多边形的实例源码程序 > VC画线源码,画多边形的实例源码程序/code/tView.cpp

    //Download by http://www.NewXing.com
// tView.cpp : implementation of the CTView class
//

#include "stdafx.h"
#include "t.h"

#include "tDoc.h"
#include "tView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTView

IMPLEMENT_DYNCREATE(CTView, CView)

BEGIN_MESSAGE_MAP(CTView, CView)
	//{{AFX_MSG_MAP(CTView)
	ON_WM_LBUTTONDBLCLK()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CTView construction/destruction

CTView::CTView()
{
	// TODO: add construction code here
	index = 1;
	first = true;
	move = false;
}

CTView::~CTView()
{
}

BOOL CTView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTView drawing

void CTView::OnDraw(CDC* pDC)
{
	CTDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CTView printing

BOOL CTView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTView diagnostics

#ifdef _DEBUG
void CTView::AssertValid() const
{
	CView::AssertValid();
}

void CTView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTDoc* CTView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTDoc)));
	return (CTDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTView message handlers

void CTView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	dc.MoveTo(v[index]);
	dc.LineTo(v[0]);
	dc.SelectStockObject(GRAY_BRUSH);
	dc.Polygon(v,index+1);
	first = true;
	move = false;
	index = 1;
	CView::OnLButtonDblClk(nFlags, point);
}

void CTView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	if (first)
	{
		v[0] = v[1] = point;
		first = false;
	}
	else
	{
		v[++index] = point;
		if (index>=30)
		{	MessageBox("数组 CPoint v[30] 下标越界!\a");
			return;
		}
		dc.MoveTo(v[index-1]);
		dc.LineTo(v[index]);
	}
	CView::OnLButtonDown(nFlags, point);
}

void CTView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	if (nFlags == MK_LBUTTON)
	{
		move = true;
		dc.SetROP2(R2_NOTXORPEN);
		dc.MoveTo(v[index-1]);
		dc.LineTo(v[index]);
		v[index] = point;
		dc.MoveTo(v[index-1]);
		dc.LineTo(v[index]);
	}
	CView::OnMouseMove(nFlags, point);
}

void CTView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	dc.MoveTo(v[index-1]);
	dc.LineTo(v[index]);
	if (!move)
		first = true;
	CView::OnLButtonUp(nFlags, point);
}