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

📄 createrect.cpp

📁 绘完直线、矩形、圆和圆弧后
💻 CPP
字号:
#include "stdafx.h"
#include "math.h"
#include "VCad.h"
#include "VCadDoc.h"
#include "VCadView.h"
#include "MainFrm.h"
#include "Entity.h"
#include "Command.h"
#include "CreateCmd.h"

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

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 ;
}

⌨️ 快捷键说明

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