⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawview.cpp

📁 一个简单的绘图程序
💻 CPP
字号:
// DrawView.cpp : implementation of the CDrawView class
//

#include "stdafx.h"
#include "Draw.h"

#include "DrawDoc.h"
#include "DrawView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDrawView

IMPLEMENT_DYNCREATE(CDrawView, CView)

BEGIN_MESSAGE_MAP(CDrawView, CView)
	//{{AFX_MSG_MAP(CDrawView)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_BYHAND, OnByhand)
	ON_COMMAND(ID_COLOR, OnColor)
	ON_COMMAND(ID_ELLIPSE, OnEllipse)
	ON_COMMAND(ID_LINE, OnLine)
	ON_COMMAND(ID_RECTANGLE, OnRectangle)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CDrawView construction/destruction

CDrawView::CDrawView()
{
	// TODO: add construction code here
	color=RGB(0,0,0);
	choice=0;
	isCaptured=false;

}

CDrawView::~CDrawView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDrawView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CDrawView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDrawView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDrawView message handlers

void CDrawView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	ptFrom=point;
	SetCapture();
	isCaptured=true;
	CView::OnLButtonDown(nFlags, point);
}

void CDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//ptTo=point;
	ReleaseCapture();
	isCaptured=false;
	//Invalidate();
	CView::OnLButtonUp(nFlags, point);
}

void CDrawView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(isCaptured)
	{
		CClientDC dc(this),dccover(this);
		CPen penDraw,penCover,*ppenOld1,*ppenOld2;
		penDraw.CreatePen(PS_SOLID,1,color);
		penCover.CreatePen(PS_SOLID,1,RGB(255,255,255));
		ppenOld1=dc.SelectObject(&penDraw);
		ppenOld2=dccover.SelectObject(&penCover);
		switch(choice)
		{
		case 1:
			dc.MoveTo(ptFrom);
			dccover.MoveTo(ptFrom);
			//ptTo=point;
			dccover.LineTo(ptTo);
			dc.LineTo(point);
			ptTo=point;
			break;
		case 2:
			dccover.Rectangle(ptFrom.x,ptFrom.y,ptTo.x,ptTo.y);
			dc.Rectangle(ptFrom.x,ptFrom.y,point.x,point.y);
			ptTo=point;
			break;
		case 3:
			dccover.Ellipse(ptFrom.x,ptFrom.y,ptTo.x,ptTo.y);
			dc.Ellipse(ptFrom.x,ptFrom.y,point.x,point.y);
			ptTo=point;
			break;
		case 4:
			dc.MoveTo(ptFrom);
			dc.LineTo(point);
			ptFrom=point;
			break;
		}
		dc.SelectObject(ppenOld1);
		dccover.SelectObject(ppenOld2);

	}
	CView::OnMouseMove(nFlags, point);
}

void CDrawView::OnByhand() 
{
	// TODO: Add your command handler code here
	choice=4;
}

void CDrawView::OnColor() 
{
	// TODO: Add your command handler code here
	CColorDialog dlg(color);
	if(dlg.DoModal()==IDOK)
	{
		color=dlg.GetColor();
	}
}

void CDrawView::OnEllipse() 
{
	// TODO: Add your command handler code here
	choice=3;
}

void CDrawView::OnLine() 
{
	// TODO: Add your command handler code here
	choice=1;
}

void CDrawView::OnRectangle() 
{
	// TODO: Add your command handler code here
	choice=2;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -