www.pudn.com > my2cadtest07261930.rar > Rectangle.cpp, change:2006-07-25,size:8874b


// Rectangle.cpp: implementation of the CRectangle class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "Rectangle.h" 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
 
 
CRectangle::CRectangle(CSuperPoint begpt,CSuperPoint endpt):CShape() 
{ 
   
  POINT potLT(begpt.GetPoint()); 
  POINT potRT = {endpt.GetPoint().x ,begpt.GetPoint().y }; 
  POINT potRB = {endpt.GetPoint().x , endpt.GetPoint().y }; 
  POINT potLB = {begpt.GetPoint().x , endpt.GetPoint().y }; 
  m_point[0] = potLT; 
  m_point[1] = potRT; 
  m_point[2] = potRB; 
  m_point[3] = potLB; 
   
  this->m_whatshape=Rect; 
  m_pointcount=4; 
  this->CalcCenterPoint(); 
  this->CalcCenterRECT(); 
} 
 
 
CRectangle::CRectangle(CSuperPoint pt1,CSuperPoint pt2,CSuperPoint pt3,CSuperPoint pt4,bool ispara):CShape() 
{ 
  if(ispara) 
  { 
	this->m_whatshape=Parallelogram; 
  } 
  else 
  { 
    this->m_whatshape=Parallel; 
  } 
  m_point[0]=pt1; 
  m_point[1]=pt2; 
  m_point[2]=pt3; 
  m_point[3]=pt4; 
 
  m_pointcount=4;   
  this->CalcCenterPoint(); 
  this->CalcCenterRECT(); 
} 
 
CRectangle::CRectangle(POINT *point,int ptcount, LOGPEN &pen, LOGBRUSH &brush):CShape() 
{ 
  for(int nI=0;nI<ptcount;nI++) 
  { 
	  m_point[nI]=point[nI]; 
  } 
 
	m_penstruct.lopnColor=pen.lopnColor; 
	m_penstruct.lopnStyle=pen.lopnStyle; 
	m_penstruct.lopnWidth.x=pen.lopnWidth.x; 
	m_penstruct.lopnWidth.y=pen.lopnWidth.y; 
 
	m_brushstruct.lbColor=brush.lbColor; 
	m_brushstruct.lbStyle=brush.lbStyle; 
	m_brushstruct.lbHatch=brush.lbHatch; 
 
	m_pointcount=ptcount; 
	this->m_whatshape=Rect; 
	this->CalcCenterPoint(); 
	this->CalcCenterRECT(); 
 
} 
 
CRectangle::CRectangle() 
{ 
 
} 
 
CRectangle::~CRectangle() 
{ 
 
} 
 
 
void CRectangle::CalcCenterPoint() 
{ 
  POINT curpoint; 
 
  curpoint.x=(m_point[0].GetPoint().x+m_point[1].GetPoint().x+m_point[2].GetPoint().x+m_point[3].GetPoint().x)/4; 
  curpoint.y=(m_point[0].GetPoint().y+m_point[1].GetPoint().y+m_point[2].GetPoint().y+m_point[3].GetPoint().y)/4;	 
  m_centerpoint=curpoint; 
} 
 
void CRectangle::CalcCenterRECT() 
{ 
     
	UINT nDistance; 
    UINT nDistance1; 
    UINT nDistance2; 
	nDistance1 = m_centerpoint.DistanceTo(m_point[0]); 
    nDistance2 = m_centerpoint.DistanceTo(m_point[1]); 
 
    nDistance = nDistance1 > nDistance2 ? nDistance1:nDistance2; 
	 
	m_rect.left = m_centerpoint.GetPoint().x - nDistance - 4; 
	m_rect.top  = m_centerpoint.GetPoint().y - nDistance - 4; 
	m_rect.right = m_centerpoint.GetPoint().x + nDistance + 4; 
	m_rect.bottom = m_centerpoint.GetPoint().y + nDistance + 4; 
} 
 
void CRectangle::PreDraw(HDC &hdc) 
{ 
  POINT pt[100]; 
	for(int nI=0;nI<4;nI++) 
	{ 
      pt[nI]=m_copypoint[nI].GetPoint(); 
	} 
	switch(m_whatshape) 
	   { 
	     case Rect:  //矩形 		 
			 ::Polygon(hdc,pt,m_pointcount);  
			 break; 
         case Parallelogram: //平行四边形  		 
			::Polygon(hdc,pt,m_pointcount);  
			break; 
	     case Parallel: //平行线 
			   { 
				::MoveToEx(hdc,m_copypoint[0].GetPoint().x,m_copypoint[0].GetPoint().y,NULL);		 
				::LineTo(hdc,m_copypoint[1].GetPoint().x,m_copypoint[1].GetPoint().y); 
 
				::MoveToEx(hdc,m_copypoint[2].GetPoint().x,m_copypoint[2].GetPoint().y,NULL);		 
				::LineTo(hdc,m_copypoint[3].GetPoint().x,m_copypoint[3].GetPoint().y); 
			   } 
			break; 
       default: 
		   break; 
	   } 
  
} 
 
 
 
CShape * CRectangle::GetShapeCopy() 
{ 
  CShape * pcopy=new CRectangle; 
  *pcopy=*this;   //值拷贝 
   
  return pcopy ; 
 
} 
 
 
bool CRectangle::IsBound(CSuperPoint &curPt) 
{ 
 for(int nI=0;nI<m_pointcount-1;nI++) 
	 { 
		if(curPt.IsOnLine(m_point[nI],m_point[nI+1])) 
			return true; 
	 }	  
 return false; 
 
} 
 
void CRectangle::SetMark(HDC &hdc) 
{ 
   for(int nI=0;nI<m_pointcount;nI++) 
   { 
    m_point[nI].SetMark(hdc); 
   } 
	 
 
} 
 
void CRectangle::ClearMark(HDC &hdc) 
{ 
    for(int nI=0;nI<m_pointcount;nI++) 
	   { 
		m_point[nI].ClearMark(hdc); 
	   } 
			 
} 
 
void CRectangle::Lock(HDC &hdc) 
{ 
	this->m_selected = true;	//已锁定 
	for(int nI=0;nI<m_pointcount;nI++) 
	{ 
	 m_point[nI].SetLockMark(hdc); 
	} 
} 
 
void CRectangle::UnLock(HDC &hdc) 
{ 
	this->m_selected = false; 
    for(int nI=0;nI<m_pointcount;nI++) 
	{ 
	 m_point[nI].ClearLockMark(hdc); 
	} 
	 
} 
 
void CRectangle::EnterOper(HDC &hdc)   
{ 
    	    
     for(int nI=0;nI<m_pointcount;nI++) 
	 { 
	 m_point[nI]=m_copypoint[nI];	   
	 } 
	 ::InvalidateRect(g_hwnd,&m_rect,true); 
 
     penattrib->GetPen(hdc,false,false); 
     DrawSelf(hdc);  
   
    this->CalcCenterPoint(); 
    this->CalcCenterRECT(); 
 
   ::InvalidateRect(g_hwnd,&m_rect,true); 
 
	m_operStatus=FirstOper; 
 
   penattrib->ReleasePen(false,false); 
 
} 
 
void CRectangle::DrawSelf(HDC &hdc) 
{ 
   POINT pt[100]; 
	for(int nI=0;nI<m_pointcount;nI++) 
	{ 
      pt[nI]=m_point[nI].GetPoint(); 
	} 
	switch(m_whatshape) 
	   { 
	   case Rect:  //矩形 		 
		::Polygon(hdc,pt,m_pointcount);  
		 break; 
       case Parallelogram: //平行四边形  		 
		::Polygon(hdc,pt,m_pointcount);  
		 break; 
	   case Parallel: //平行线 
		   { 
			::MoveToEx(hdc,m_point[0].GetPoint().x,m_point[0].GetPoint().y,NULL);		 
			::LineTo(hdc,m_point[1].GetPoint().x,m_point[1].GetPoint().y); 
 
			::MoveToEx(hdc,m_point[2].GetPoint().x,m_point[2].GetPoint().y,NULL);		 
			::LineTo(hdc,m_point[3].GetPoint().x,m_point[3].GetPoint().y); 
		   } 
		break; 
       default: 
		   break; 
	   } 
	if(m_selected)   //如果这是一个被选中图形 在操作后的图形上打上标记 
   { 
    m_point[0].SetStatus(); 
    m_point[1].SetStatus(); 
    m_point[2].SetStatus(); 
    m_point[3].SetStatus(); 
    SetMark(hdc); 
    Lock(hdc); 
   } 
} 
 
void CRectangle::SavetoEmf(HDC &hdc) 
{ 
  	if	( 
		penattrib->GetPenStyle().lopnColor!=m_penstruct.lopnColor|| 
		penattrib->GetPenStyle().lopnWidth.x!=m_penstruct.lopnWidth.x|| 
		penattrib->GetPenStyle().lopnStyle!=m_penstruct.lopnStyle 
		) 
		{ 
		    penattrib->ReleasePen(false); 
			penattrib->SetPenStyle(m_penstruct); 
			penattrib->GetPen(hdc,false); 
		} 
 
	if	( 
		penattrib->GetBrushStyle().lbColor!=m_brushstruct.lbColor|| 
		penattrib->GetBrushStyle().lbHatch!=m_brushstruct.lbHatch|| 
		penattrib->GetBrushStyle().lbStyle!=m_brushstruct.lbStyle 
		) 
		{ 
		    penattrib->ReleaseBrush(); 
			penattrib->SetBrushStyle(m_brushstruct); 
			penattrib->GetBrush(hdc); 
		} 
 
   POINT pt[100]; 
	for(int nI=0;nI<m_pointcount;nI++) 
	{ 
      pt[nI]=m_point[nI].GetPoint(); 
	} 
	switch(m_whatshape) 
	   { 
	   case Rect:  //矩形 		 
		::Polygon(hdc,pt,m_pointcount);  
		 break; 
       case Parallelogram: //平行四边形  		 
		::Polygon(hdc,pt,m_pointcount);  
		 break; 
	   case Parallel: //平行线 
		   { 
 
			 ::Polygon(hdc,pt,2);   
//			::MoveToEx(hdc,m_point[0].GetPoint().x,m_point[0].GetPoint().y,NULL);		 
//			::LineTo(hdc,m_point[1].GetPoint().x,m_point[1].GetPoint().y); 
             ::Polygon(hdc,pt+2,2);  
//			::MoveToEx(hdc,m_point[2].GetPoint().x,m_point[2].GetPoint().y,NULL);		 
//			::LineTo(hdc,m_point[3].GetPoint().x,m_point[3].GetPoint().y); 
		   } 
		break; 
       default: 
		   break; 
	   } 
		 
} 
void CRectangle::MoveSelf(HDC &hdc,int dx,int dy) 
{ 
    penattrib->GetPen(hdc,true); 
 
    if(m_operStatus==FirstOper) 
	{	 		 
      m_operStatus=NoFirstOper;	 
	} 
    else 
	{ 
      PreDraw(hdc);  
	} 
	 
    for(int nI=0;nI<m_pointcount;nI++) 
	{ 
		m_copypoint[nI]=m_point[nI]; 
        m_copypoint[nI].Move(dx,dy); 
	}	 
	 PreDraw(hdc);  
    penattrib->ReleasePen(true); 
} 
 
void CRectangle::ZoomSelf(HDC &hdc,double scale) 
{ 
   penattrib->GetPen(hdc,true); 
 
    if(m_operStatus==FirstOper) 
	{	 		 
      m_operStatus=NoFirstOper;	 
	} 
    else 
	{ 
      PreDraw(hdc);  
	} 
	 
    for(int nI=0;nI<m_pointcount;nI++) 
	{ 
		m_copypoint[nI]=m_point[nI]; 
        m_copypoint[nI].ZoomIn(scale,m_centerpoint); 
	}	 
	 PreDraw(hdc);  
    penattrib->ReleasePen(true); 
 
} 
 
 
void CRectangle::RotateSelf(HDC &hdc,double angle) 
{ 
    penattrib->GetPen(hdc,true); 
 
    if(m_operStatus==FirstOper) 
	{	 		 
      m_operStatus=NoFirstOper;	 
	} 
    else 
	{ 
      PreDraw(hdc);  
	} 
	 
    for(int nI=0;nI<m_pointcount;nI++) 
	{ 
		m_copypoint[nI]=m_point[nI]; 
        m_copypoint[nI].RotateOnFix(this->GetCenterPoint(),angle); 
	}	 
	 PreDraw(hdc);  
     penattrib->ReleasePen(true); 
} 
 
 
void CRectangle::MirrorSelf(HDC &hdc,double angle,CSuperPoint &ptPos) 
{ 
    penattrib->GetPen(hdc,true); 
 
    if(m_operStatus==FirstOper) 
	{	 		 
      m_operStatus=NoFirstOper;	 
	} 
    else 
	{ 
      PreDraw(hdc);  
	} 
	 
    for(int nI=0;nI<m_pointcount;nI++) 
	{ 
		m_copypoint[nI]=m_point[nI]; 
        m_copypoint[nI].MirrorOnAxis(angle,ptPos); 
	}	 
	 PreDraw(hdc);  
 
     penattrib->ReleasePen(true); 
}