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

📄 shape.cpp

📁 在PDA中的画图程序
💻 CPP
字号:
// Shape.cpp: implementation of the CShape class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DrawInPDA.h"
#include "Shape.h"
#include "DrawInPDAView.h"

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

extern CDrawInPDAView *p_View;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CShape::CShape()
{

}

CShape::~CShape()
{

}

void CShape::Draw(CDC *pDC)
{

}
bool CShape::Intersects(const CRect &rect)
{
    ASSERT_VALID(this);
	CRect fixed = m_extent;
	fixed.NormalizeRect();
	CRect rectT = rect;
	rectT.NormalizeRect();
	return !(rectT & fixed).IsRectEmpty();
}

//////////////////////////
CShapePoly::CShapePoly()
{
	m_sPoints = NULL;
	m_nPoints = 0;
	m_nAllocPoints = 0;
}

CShapePoly::~CShapePoly()
{
	if (m_sPoints != NULL)
		delete[] m_sPoints;
}

void CShapePoly::AddPoint(const CGeoPoint& Point, CDrawInPDAView* pView)
{
	ASSERT_VALID(this);
	if (m_nPoints == m_nAllocPoints)
	{
		CGeoPoint* newPoints = new CGeoPoint[m_nAllocPoints + 10];
		if (m_sPoints != NULL)
		{
		memcpy(newPoints, m_sPoints, sizeof(CGeoPoint) *m_nAllocPoints);//Copies characters between buffers.
		delete[] m_sPoints;//The memcpy function copies count bytes of src to dest
		}
		m_sPoints = newPoints;
		m_nAllocPoints += 10;
		
	}

	if (m_nPoints == 0 || (m_sPoints[m_nPoints - 1].x!=Point.x)||(m_sPoints[m_nPoints - 1].y!=Point.y))
	{
		m_sPoints[m_nPoints++] = Point;
		//if (!RecalcBounds(pView))
		//{
		//	if (pView == NULL)
		//		Invalidate();
		//	else
		//		pView->InvalObj(this);
		//}
		//m_pDocument->SetModifiedFlag();
	}

	
}

void CShapePoly::Draw(CDC *pDC)
{   
	int i,x,y,xStart,yStart;
	CPoint* m_PolygonPoints;
	m_PolygonPoints=new CPoint[m_nPoints];
  // Draw with a thick blue pen.
  CPen penBlue(PS_SOLID, 5, RGB(0, 0, 255));
  CPen* pOldPen = pDC->SelectObject(&penBlue);
  // And a solid red brush.
  CBrush brushRed(RGB(255, 0, 0));
  CBrush* pOldBrush = pDC->SelectObject(&brushRed);
	p_View->DPtoVP(p_View->m_xStart,p_View->m_yStart,&xStart,&yStart);
	for(i=0;i<m_nPoints;i++)
	{
	p_View->DPtoVP(m_sPoints[i].x,m_sPoints[i].y,&x,&y);
	x=(x-xStart);
	y=(y-yStart);
	m_PolygonPoints[i].x=x;
	m_PolygonPoints[i].y=y;
	}
	pDC->Polygon(m_PolygonPoints, m_nPoints);
	pDC->SelectObject(pOldPen);
    pDC->SelectObject(pOldBrush);
	delete  m_PolygonPoints;	
}

//////////////////
CShapePoint::CShapePoint()
{

}

CShapePoint::~CShapePoint()
{

}

void CShapePoint::Draw(CDC *pDC)
{	
	int x1,y1,x0,y0;
	CPen Pen(0, PS_SOLID, RGB(0,0,0));
	CPen* pOldPen = pDC->SelectObject(&Pen);
	
	//	pDC->SetPixel(point,RGB(0,0,0));
   	p_View->DPtoVP(m_gPoint.x,m_gPoint.y,&x1,&y1);
	p_View->DPtoVP(p_View->m_xStart,p_View->m_yStart,&x0,&y0);
	x1=x1-x0;
	y1=y1-y0;
	pDC->MoveTo(x1 - 2, y1);
	pDC->LineTo(x1+3, y1);
	pDC->MoveTo(x1, y1 - 2);
	pDC->LineTo(x1, y1+3);
//	pDC->SetPixel(point,RGB(255,0,0));
	pDC->SelectObject(pOldPen);

}

//////////////
CShapeLine::CShapeLine()
{

}

CShapeLine::~CShapeLine()
{

}

void CShapeLine::Draw(CDC *pDC)
{	int x1,x2,x0,y1,y2,y0; 
//	CPen Pen(0, PS_SOLID, RGB(255,0,0));
//	CPen* pOldPen = pDC->SelectObject(&Pen);
	p_View->DPtoVP(m_sPoint[0].m_gPoint.x,m_sPoint[0].m_gPoint.y,&x1,&y1);
	p_View->DPtoVP(m_sPoint[1].m_gPoint.x,m_sPoint[1].m_gPoint.y,&x2,&y2);
	p_View->DPtoVP(p_View->m_xStart,p_View->m_yStart,&x0,&y0);
	x1=x1-x0;
	y1=y1-y0;
	x2=x2-x0;
	y2=y2-y0;
	pDC->MoveTo(x1 , y1);
	pDC->LineTo(x2 , y2);
//	pDC->SelectObject(pOldPen);
//	m_sPoint[0].Draw(pDC);
//	m_sPoint[1].Draw(pDC);

}

///////////////
CShapeRectangle::CShapeRectangle()
{
	m_sPoints = new CShapePoint[2];

}

CShapeRectangle::~CShapeRectangle()
{
	if (m_sPoints != NULL)
		delete[] m_sPoints;

}

void CShapeRectangle::Draw(CDC *pDC)
{
    // Create and select a solid blue brush.
	int x1,y1,x2,y2,x0,y0;
   CBrush brushBlue(RGB(0, 0, 255));
   CBrush* pOldBrush = pDC->SelectObject(&brushBlue);

   // Create and select a thick, black pen.
   CPen penBlack;
   penBlack.CreatePen(PS_SOLID, 3, RGB(0, 0, 0));
   CPen* pOldPen = pDC->SelectObject(&penBlack);
	p_View->DPtoVP(m_sPoints[0].m_gPoint.x,m_sPoints[0].m_gPoint.y,&x1,&y1);
	p_View->DPtoVP(m_sPoints[1].m_gPoint.x,m_sPoints[1].m_gPoint.y,&x2,&y2);
	p_View->DPtoVP(p_View->m_xStart,p_View->m_yStart,&x0,&y0);
	x1=x1-x0;
	y1=y1-y0;
	x2=x2-x0;
	y2=y2-y0;   
  // RECT rctA = {m_sPoints[0].m_gPoint.X,m_sPoints[0].m_gPoint.Y,m_sPoints[1].m_gPoint.X,m_sPoints[1].m_gPoint.Y};
	pDC->Rectangle(x1,y1,x2,y2);
   // Put back the old objects.
   pDC->SelectObject(pOldBrush);
   pDC->SelectObject(pOldPen);
}






⌨️ 快捷键说明

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