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

📄 mydrawview.cpp

📁 计算机图形学实验中综合使用鼠标、菜单、橡皮条等交互技术实现直线、圆和矩形等基本图形的绘制
💻 CPP
字号:
// MyDrawView.cpp : implementation of the CMyDrawView class
//

#include "stdafx.h"
#include "MyDraw.h"

#include "MyDrawDoc.h"
#include "MyDrawView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyDrawView
int x1,y1;
int x2,y2;
int DocRed,DocBlue,DocGreen;
IMPLEMENT_DYNCREATE(CMyDrawView, CView)

BEGIN_MESSAGE_MAP(CMyDrawView, CView)
	//{{AFX_MSG_MAP(CMyDrawView)
	ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_DRAW_CIRCLE, OnDrawCircle)
	ON_COMMAND(ID_DRAW_QUERE, OnDrawQuere)
	ON_COMMAND(ID_DRAW_JUXING, OnDrawJuxing)
	ON_COMMAND(ID_DRAW_BIG, OnDrawBig)
	ON_COMMAND(ID_DROP_MID, OnDropMid)
	ON_COMMAND(ID_DROP_SMALL, OnDropSmall)
	ON_COMMAND(ID_DRAW_COLOR, OnDrawColor)
	ON_COMMAND(ID_BACK_SMALL, OnBackSmall)
	ON_COMMAND(ID_BACK_MID, OnBackMid)
	ON_COMMAND(ID_BACK_BIG, OnBackBig)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMyDrawView construction/destruction

CMyDrawView::CMyDrawView()
{
	// TODO: add construction code here
	select=4;
	flag=0;
	width=0;
	red=0;
	green=0;
	blue=0;
}

CMyDrawView::~CMyDrawView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyDrawView drawing

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

	int i=0;
for(int j=0;j<1;j++){
	int color=RGB(red,green,blue);
	CPen newpen(PS_SOLID,width,color);
	CPen*oldpen=pDC->SelectObject(&newpen);

	switch(select){
		case 1:
		
		pDC->MoveTo(x1,y1);
		pDC->LineTo(pointtem);
		 break;  
		case 2:
			
			pDC->Ellipse(ret);
			break;
		case 3:
			
			pDC->Rectangle(ret);
			break;
		case 4:
			break;	
		}

	
	/*for(i=0;i<pDoc->m_Pointend.GetSize();i++){
			pDC->MoveTo(pDoc->m_Pointsta[i]);
			pDC->LineTo(pDoc->m_Pointend[i]);
		
		}
	for(i=0;i<pDoc->m_RectCircle.GetSize();i++){
				pDC->Ellipse(pDoc->m_RectCircle[i]);
			}
	for(i=0;i<pDoc->m_RectJuxing.GetSize();i++){
				pDC->Rectangle(pDoc->m_RectJuxing[i]);
			}
	for(i=0;i<pDoc->m_Pointag.GetSize()-1;i++){
		if(pDoc->m_Pointag[i].x!=-1&&pDoc->m_Pointag[i+1].x!=-1){
				pDC->MoveTo(pDoc->m_Pointag[i]);
				pDC->LineTo(pDoc->m_Pointag[i+1]);

			}
	}*/
	
	for(i=0;i<pDoc->m_AllPoint.GetSize()-1;i++){
		if(pDoc->m_AllPoint[i].style==1&&pDoc->m_AllPoint[i+1].style==1){
			if(i%2==0){
			
			CPen newpen1(PS_SOLID,pDoc->m_AllPoint[i].width,pDoc->m_AllPoint[i].color);
			CPen*oldpen=pDC->SelectObject(&newpen1);
			pDC->MoveTo(pDoc->m_AllPoint[i].point);
			pDC->LineTo(pDoc->m_AllPoint[i+1].point);
			}
		}
		else if (pDoc->m_AllPoint[i].style==2&&pDoc->m_AllPoint[i+1].style==2) {
			if(i%2==0){
				CPen newpen2(PS_SOLID,pDoc->m_AllPoint[i].width,pDoc->m_AllPoint[i].color);
				CPen*oldpen=pDC->SelectObject(&newpen2);
				ret.SetRect(pDoc->m_AllPoint[i].point,pDoc->m_AllPoint[i+1].point);
				pDC->Ellipse(ret);
			}
			}
		else if(pDoc->m_AllPoint[i].style==3&&pDoc->m_AllPoint[i+1].style==3){
			if(i%2==0){
				CPen newpen3(PS_SOLID,pDoc->m_AllPoint[i].width,pDoc->m_AllPoint[i].color);
				CPen*oldpen=pDC->SelectObject(&newpen3);
				ret.SetRect(pDoc->m_AllPoint[i].point,pDoc->m_AllPoint[i+1].point);
				pDC->Rectangle(ret);
			}
			}
		else if(pDoc->m_AllPoint[i].style==4&&pDoc->m_AllPoint[i+1].style==4){
				CPen newpen4(PS_SOLID,pDoc->m_AllPoint[i].width,pDoc->m_AllPoint[i].color);
				CPen*oldpen=pDC->SelectObject(&newpen4);
				if(pDoc->m_AllPoint[i].point.x!=-1&&pDoc->m_AllPoint[i+1].point.y!=-1){
					pDC->MoveTo(pDoc->m_AllPoint[i].point);
					pDC->LineTo(pDoc->m_AllPoint[i+1].point);
				}
			}
		
	}
	
	pDC->SelectObject(oldpen);
}
}
/////////////////////////////////////////////////////////////////////////////
// CMyDrawView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMyDrawView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMyDrawView message handlers

void CMyDrawView::OnDrawLine() 
{
	// TODO: Add your command handler code here
	select=1;
	MessageBox("现在画直线!");

}

void CMyDrawView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMyDrawDoc*PDoc=GetDocument();
	
	flag=1;
	
	switch (select){
	case 0:
		MessageBox("please choose style first!");
		break;
	case 1:
		x1=point.x;
		y1=point.y;

		
		break;
	case 2:
		x1=point.x;
		y1=point.y;
		
		break;
	case 3:
		x1=point.x;
		y1=point.y;
		break;
	case 4:
		x1=point.x;
		y1=point.y;
		pointtem.x=x1;
		pointtem.y=y1;
		break;

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

void CMyDrawView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMyDrawDoc*PDoc=GetDocument();
	CClientDC dc(this);
	CRect rc;
	GetClientRect(&rc);
	if(flag==1){
		for(int j=0;j<1;j++){
		int color=RGB(red,green,blue);
		CPen newpen(PS_SOLID,width,color);
		CPen*oldpen=dc.SelectObject(&newpen);
		switch (select){
	case 0:
		
		break;
	case 1:
		//dc.MoveTo(x1,y1);
		//dc.LineTo(point);
		Invalidate();
		pointtem=point;
		
		break;
	case 2:
		pointtem.x=x1;
		pointtem.y=y1;
		ret.SetRect(pointtem,point);
		InvalidateRect(ret);
		dc.Ellipse(ret);
		//InvalidateRect(ret);	
		
		
		
		
		break;
	case 3:
		pointtem.x=x1;
		pointtem.y=y1;
		ret.SetRect(pointtem,point);
		InvalidateRect(ret);
		dc.Rectangle(ret);
		//InvalidateRect(ret);
		
		
		break;
	case 4:
		
		dc.MoveTo(pointtem);
		dc.LineTo(point);
		PDoc->m_Pointag.Add(pointtem);
		data.point.x=pointtem.x;
		data.point.y=pointtem.y;
		data.width=width;
		data.style=4;
		data.color=RGB(red,green,blue);
		PDoc->m_AllPoint.Add(data);
		pointtem=point;
		
		//Invalidate();
		break;
		}
		dc.SelectObject(oldpen);
	}
	}
	CView::OnMouseMove(nFlags, point);
}

void CMyDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMyDrawDoc*PDoc=GetDocument();
	if(flag==1){
		flag=0;
		CClientDC dc(this);
		CRect rc;
		GetClientRect(&rc);
		int color=RGB(red,green,blue);
		//CPen newpen(PS_SOLID,width,color);
		
		switch (select){
	case 0:
		
		break;
	case 1:
		pointtem.x=x1;
		pointtem.y=y1;
		x2=point.x;
		y2=point.y;
		PDoc->m_Pointsta.Add(pointtem);
		PDoc->m_Pointend.Add(point);
		data.point.x=x1;
		data.point.y=y1;
		data.width=width;
		data.style=1;
		data.color=RGB(red,green,blue);
		PDoc->m_AllPoint.Add(data);
		data.point.x=point.x;
		data.point.y=point.y;
		data.width=width;
		data.style=1;
		data.color=RGB(red,green,blue);
		PDoc->m_AllPoint.Add(data);
		
		break;
	case 2:
		
		ret.SetRect(pointtem,point);
		PDoc->m_RectCircle.Add(ret);
		data.point.x=pointtem.x;
		data.point.y=pointtem.y;
		data.width=width;
		data.style=2;
		data.color=RGB(red,green,blue);
		PDoc->m_AllPoint.Add(data);
		data.point.x=point.x;
		data.point.y=point.y;
		data.width=width;
		data.style=2;
		data.color=RGB(red,green,blue);
		PDoc->m_AllPoint.Add(data);
		break;
	case 3:
		ret.SetRect(pointtem,point);
		PDoc->m_RectJuxing.Add(ret);
		data.point.x=pointtem.x;
		data.point.y=pointtem.y;
		data.width=width;
		data.style=3;
		data.color=RGB(red,green,blue);
		PDoc->m_AllPoint.Add(data);
		data.point.x=point.x;
		data.point.y=point.y;
		data.width=width;
		data.style=3;
		data.color=RGB(red,green,blue);
		PDoc->m_AllPoint.Add(data);
		break;
	case 4:
		PDoc->m_Pointag.Add(pointtem);
		
		data.point.x=pointtem.x;
		data.point.y=pointtem.y;
		data.width=width;
		data.style=4;
		data.color=RGB(red,green,blue);
		PDoc->m_AllPoint.Add(data);

		CPen newpen(PS_SOLID,width,color);
		CPen*oldpen=dc.SelectObject(&newpen);
		dc.MoveTo(pointtem);
		dc.LineTo(point);

		data.point.x=point.x;
		data.point.y=point.y;
		data.width=width;
		data.style=4;
		data.color=RGB(red,green,blue);
		PDoc->m_AllPoint.Add(data);

		data.point.x=-1;
		data.point.y=-1;
		PDoc->m_AllPoint.Add(data);
		break;
		dc.SelectObject(oldpen);
	}
	Invalidate();	
	}	
	CView::OnLButtonUp(nFlags, point);
}

void CMyDrawView::OnDrawCircle() 
{
	// TODO: Add your command handler code here
	select=2;
	MessageBox("现在画椭圆!");
}



void CMyDrawView::OnDrawJuxing() 
{
	// TODO: Add your command handler code here
	select=3;
	MessageBox("现在画矩形!");
}
void CMyDrawView::OnDrawQuere() 
{
	// TODO: Add your command handler code here
	select=4;
	MessageBox("现在画任意线!");
}


void CMyDrawView::OnDrawBig() 
{
	// TODO: Add your command handler code here
	width=8;
}

void CMyDrawView::OnDropMid() 
{
	// TODO: Add your command handler code here
	width=3;
}

void CMyDrawView::OnDropSmall() 
{
	// TODO: Add your command handler code here
	width=1;
}

void CMyDrawView::OnDrawColor() 
{
	// TODO: Add your command handler code here
		CPenColor ColorDlg;
		
	if(ColorDlg.DoModal()==IDOK){
		
		red=ColorDlg.m_nRed;
		blue=ColorDlg.m_nBlue;
		green=ColorDlg.m_nGreen;
	}
}

void CMyDrawView::OnBackSmall() 
{
	// TODO: Add your command handler code here
	select=4;
	width=1;
	red=255;
	blue=255;
	green=255;
}

void CMyDrawView::OnBackMid() 
{
	// TODO: Add your command handler code here
	select=4;
	width=3;
	red=255;
	blue=255;
	green=255;
}

void CMyDrawView::OnBackBig() 
{
	// TODO: Add your command handler code here
	select=4;
	width=8;
	red=255;
	blue=255;
	green=255;
}

⌨️ 快捷键说明

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