www.pudn.com > jtlab.rar > Rectangle.cpp


#include "stdafx.h" 
#include "math.h" 
#include "VCad.h" 
#include "VCadDoc.h" 
#include "VCadView.h" 
#include "Entity.h" 
#include "MainFrm.h" 
#include "CreateCmd.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////// 
//define Rectangle class 
IMPLEMENT_SERIAL(CRectangle, CEntity, 0) 
 
CRectangle::CRectangle() 
{ 
	Init() ; 
} 
 
CRectangle::CRectangle(const CRectangle& rect) 
		: CEntity(rect) 
{ 
	m_LeftTop = rect.m_LeftTop ; 
	m_RightBottom = rect.m_RightBottom ; 
} 
 
CRectangle::CRectangle(const Position& LeftTop,const Position& RightBottom) 
{ 
	Init() ; 
	m_LeftTop = LeftTop ; 
	m_RightBottom = RightBottom ; 
} 
 
CRectangle::~CRectangle() 
{ 
} 
 
CEntity* CRectangle::Copy() 
{ 
	CEntity*	pEntity = new CRectangle(m_LeftTop, m_RightBottom); 
	return pEntity; 
} 
 
void CRectangle::Init() 
{ 
	CEntity::Init(); 
	m_type = etRectangle; 
	m_LeftTop.Init(); 
	m_RightBottom.Init(); 
} 
 
int CRectangle::GetType() 
{ 
	return etRectangle ; 
} 
 
Position CRectangle::GetLeftTopPos() 
{ 
	return m_LeftTop; 
} 
 
Position CRectangle::GetRightBottomPos() 
{ 
	return m_RightBottom; 
} 
/////////////////////// 
void CRectangle::Draw(CDC * pDC, int drawMode /* = dmNormal */) 
{ 
 
	CPoint sltp, srbp; 
 
	g_pView->WorldtoScreen(m_LeftTop, sltp) ; 
	g_pView->WorldtoScreen(m_RightBottom, srbp) ; 
 
	int		n = GetROP2(pDC->GetSafeHdc()); 
	// create a pen by following rules: 
	// if in normal draw mode, create a pen by its member variables  
	// if else, create a pen using global funtion "SetDrawEnvir" 
	CPen	pen;  
	if( drawMode == dmNormal )  
		pen.CreatePen(m_lineStyle,m_lineWidth,m_color) ; 
	else 
		::SetDrawEnvir(pDC, drawMode, &pen); 
	 
	CPen* pOldPen = pDC->SelectObject(&pen) ; 
	pDC->SetMapMode(MM_LOENGLISH);  
	pDC->MoveTo(sltp) ; 
	pDC->LineTo(sltp.x, srbp.y) ; 
	pDC->LineTo(srbp); 
	pDC->LineTo(srbp.x, sltp.y) ; 
	pDC->LineTo(sltp) ; 
	 
	pDC->SelectObject(pOldPen) ; 
	pDC->SetROP2(n); 
} 
 
void CRectangle::Serialize(CArchive& ar)  
{ 
	CEntity::Serialize(ar); 
	m_LeftTop.Serialize(ar); 
	m_RightBottom.Serialize(ar); 
} 
 
//class Rectangle 
CCreateRect::CCreateRect() 
	: m_LeftTop(0,0), m_RightBottom(0,0) 
{ 
	m_nStep = 0; // 初始化操作步为 0 
} 
 
CCreateRect::~CCreateRect() 
{ 
} 
 
int CCreateRect::GetType() 
{ 
	return ctCreateRectangle;  
} 
 
int	CCreateRect::OnLButtonDown(UINT nFlags, const Position& pos)  
{ 
	m_nStep ++; // 每次单击鼠标左键时操作步加 1  
	switch(m_nStep) // 根据操作步执行相应的操作 
	{ 
		case 1:  
		{ 
			m_LeftTop = m_RightBottom = pos; 
			::Prompt("请输入矩形的右下角点:") ; 
			break; 
		} 
		case 2: 
		{ 
			CDC*	pDC = g_pView->GetDC(); // 得到设备环境指针  
 
			// 擦除在拖动状态时显示的橡皮线 
			CRectangle*	pTempRect = new CRectangle(m_LeftTop, m_RightBottom);  
			pTempRect->Draw(pDC, dmDrag); 
			delete pTempRect; 
 
			m_RightBottom = pos;  
						 
			CRectangle*	pRect = new CRectangle(m_LeftTop, m_RightBottom); // 根据两点创建矩形 
			pRect->Draw(pDC, dmNormal); 
			g_pDoc->m_EntityList.AddTail(pRect); // 将指针添加到图元链表 
			g_pDoc->SetModifiedFlag(TRUE);// set modified flag ; 
			 
			g_pView->ReleaseDC(pDC); // 释放设备环境指针 
			 
			m_nStep = 0;   //将操作步重置为 0 
			::Prompt("请输入矩形的左上角点:") ; 
			break;} 
		 
	} 
	return 0; 
} 
 
int	CCreateRect::OnMouseMove(UINT nFlags, const Position& pos) 
{ 
	// 用一静态变量nPreRefresh记录进入OnMouseMove状态时的刷新次数 
	static	int nPreRefresh = g_nRefresh;  
	// 布尔变量bRefresh说明在OnMouseMove过程中视窗是否被刷新 
	BOOL	bRefresh = FALSE;  
	// nCurRefresh用于记录当前的刷新次数 
	int		nCurRefresh = g_nRefresh;  
	// 如果nCurRefresh和nPreRefresh不相等,说明视窗曾被刷新过 
	if(nCurRefresh != nPreRefresh){  
		bRefresh = TRUE; 
		nPreRefresh = nCurRefresh;  
	} 
 
	switch(m_nStep) 
	{ 
		case 0: 
			::Prompt("请输入矩形的左上角点:") ; 
			break; 
		case 1: 
		{ 
			Position	prePos, curPos; 
			prePos = m_RightBottom; // 获得鼠标所在的前一个位置 
			 
			curPos = pos; 
 
			CDC*	pDC = g_pView->GetDC(); // 得到设备环境指针 
			 
			// 创建临时对象擦除上一条橡皮线 
			CRectangle*	pTempRect = new CRectangle(m_LeftTop, prePos); 
			if(!bRefresh) // 当视窗没有被刷新时,重画原来的橡皮线使其被擦除 
				pTempRect->Draw(pDC, dmDrag); 
			delete pTempRect; 
			// 创建临时对象,根据当前位置绘制一条橡皮线 
			CRectangle*	pTempRect2 = new CRectangle(m_LeftTop, curPos); 
			pTempRect2->Draw(pDC, dmDrag); 
			delete pTempRect2; 
  
			g_pView->ReleaseDC(pDC); // 释放设备环境指针			 
 
			m_RightBottom = curPos; // 将当前位置设置为直线终点,以备下一次鼠标移动时用 
			break; 
		} 
	} 
	return 0; 
} 
// 单击鼠标右键取消当前的操作 
int	CCreateRect::OnRButtonDown(UINT nFlags, const Position& pos)  
{ 
	// 如果当前的操作步为 1 ,那么要在结束本次操作前擦除上次鼠标移动时绘制的橡皮线 
	if(m_nStep == 1){  
		CDC*	pDC = g_pView->GetDC(); // 得到设备环境指针 
		Position	prePos = m_RightBottom; // 获得鼠标所在的前一个位置 
		CRectangle*	pTempRect = new CRectangle(m_LeftTop, m_RightBottom); 
		pTempRect->Draw(pDC, dmDrag); // 擦除上一次绘制的橡皮线 
		delete pTempRect; 
		g_pView->ReleaseDC(pDC); // 释放设备环境指针 
	} 
	m_nStep = 0; // 将操作步重置为 0  
	::Prompt("请输入矩形的左上角点:") ; 
	return 0; 
} 
// 调用Cancel 函数取消本次操作 
int CCreateRect::Cancel() 
{ 
	// 如果当前的操作步为 1 ,那么要在结束本次操作前擦除上次鼠标移动时绘制的橡皮线 
	if(m_nStep == 1){  
		CDC*	pDC = g_pView->GetDC(); // 得到设备环境指针 
		Position	prePos = m_RightBottom; // 获得鼠标所在的前一个位置 
		CRectangle*	pTempRect = new CRectangle(m_LeftTop, m_RightBottom); 
		pTempRect->Draw(pDC, dmDrag); // 擦除上一次绘制的橡皮线 
		delete pTempRect; 
		g_pView->ReleaseDC(pDC); // 释放设备环境指针 
	} 
	m_nStep = 0; // 将操作步重置为 0  
	::Prompt("就绪"); // 等待提示新类型的命令操作 
	return 0 ; 
}