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

📄 mypaintdoc.cpp

📁 一个用VC++编写的绘图工具
💻 CPP
字号:
// MyPaintDoc.cpp : implementation of the CMyPaintDoc class
//

#include "stdafx.h"
#include "MyPaint.h"

#include "MyPaintDoc.h"
#include "POSITION.H"
#include "ENTITY.H"
#include "MyPaintView.h"

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

extern void Prompt (char*);
/////////////////////////////////////////////////////////////////////////////
// CMyPaintDoc

IMPLEMENT_DYNCREATE(CMyPaintDoc, CDocument)

BEGIN_MESSAGE_MAP(CMyPaintDoc, CDocument)
	//{{AFX_MSG_MAP(CMyPaintDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyPaintDoc construction/destruction

CMyPaintDoc::CMyPaintDoc()
{
	// TODO: add one-time construction code here
	g_pDoc = this;
	m_pPmtEntity = NULL; // 设置处于提示状态的元素为空

}

CMyPaintDoc::~CMyPaintDoc()
{
}

BOOL CMyPaintDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CMyPaintDoc serialization

void CMyPaintDoc::Serialize(CArchive& ar)
{
	m_EntityList.Serialize(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CMyPaintDoc diagnostics

#ifdef _DEBUG
void CMyPaintDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMyPaintDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyPaintDoc commands

void CMyPaintDoc::Draw(CDC *pDC)
{
	// 绘制链表中的图元
	POSITION	pos = m_EntityList.GetHeadPosition();
	while(pos!=NULL)
	{
		CEntity*	pEntity = (CEntity *) m_EntityList.GetNext(pos);
		pEntity->Draw(pDC, dmNormal);
	}
	// 绘制选择集中的图元
	for( int i = 0 ; i < m_selectArray.GetSize() ; i++ ){
		CEntity*	pSelEntity = (CEntity*) m_selectArray[i] ;
        pSelEntity->Draw(pDC, dmSelect) ;
	}
}

void CMyPaintDoc::OnMouseMove(UINT nFlags, const Position& pos)
{
	if(m_EntityList.GetCount() == 0)
		return;
	::Prompt("拾取图元");

	BOOL	bPicked = FALSE;
	CEntity*	pickedEntity = NULL;

	POSITION	position = m_EntityList.GetHeadPosition();
	while(position != NULL){
		CEntity*	pEntity = (CEntity *)m_EntityList.GetNext(position);

		//
		double curRadius = PICK_RADIUS / g_pView->GetScale(); 
		if( pEntity->Pick(pos, curRadius) ){ 
			bPicked = TRUE;
			pickedEntity = pEntity;
			break;
		}
	}

	CDC*	pDC = g_pView->GetDC(); // 得到视的设备环境指针
	if( bPicked ){ // 如果某个图元被拾取到
		if( m_pPmtEntity ){			
			m_pPmtEntity->Draw( pDC, dmNormal ); 
			m_pPmtEntity = NULL;
		}

		m_pPmtEntity = pickedEntity ; 

		if( ! IsSelected(m_pPmtEntity) ){ 
			//设置光标状态;
			m_pPmtEntity->LoadPmtCursor();
			m_pPmtEntity->Draw( pDC, dmPrompt );
		}
		// 如果提示图元已存在于选择集中,那么将它恢复为空
		else 
			m_pPmtEntity = NULL;
	}
	else{ // 如果没有图元被拾取到
		if( m_pPmtEntity ){ 
			m_pPmtEntity->Draw( pDC, dmNormal ); 
			m_pPmtEntity = NULL;
		}
    }
	g_pView->ReleaseDC(pDC); // 释放视的设备环境指针
}

void CMyPaintDoc::OnLButtonDown(UINT nFlags, const Position& pos)
{
	CDC*	pDC = g_pView->GetDC() ;	// 得到视的设备环境指针

	if(m_pPmtEntity){ 
		if( !(nFlags & MK_CONTROL) )		// 如果没有按下Ctrl键,则首先清空选择集
			RemoveAllSelected();
		m_pPmtEntity->Draw(pDC,dmSelect);	// 将图元绘制为选中状态
		m_selectArray.Add(m_pPmtEntity);	// 将图元放入选择集中
	}
	else{
		if( !(nFlags & MK_CONTROL) )	// 如果没有按下Ctrl键,则清空选择集
			RemoveAllSelected();
	}
	m_pPmtEntity = NULL;	// 将提示图元对象设置为空
	g_pView->ReleaseDC(pDC);// 释放视的设备环境指针
} 

// 清空选择集
void CMyPaintDoc::RemoveAllSelected()
{
	// 首先选择集中的元素绘制为正常状态,然后清空选择集
	CDC*	pDC = g_pView->GetDC();
	for( int i = 0 ; i < m_selectArray.GetSize() ; i++ ){
		CEntity*	pSelEntity = (CEntity*) m_selectArray[i] ;
        pSelEntity->Draw(pDC, dmNormal) ;
	}
	m_selectArray.RemoveAll() ;
	g_pView->ReleaseDC(pDC);
}

// 判断某一图元对象是否被选中
BOOL CMyPaintDoc::IsSelected(CEntity* pEntity)
{
	// 判断图元对象是否已经在选择集中
	if( pEntity )
	{
		for( int i = 0 ; i < m_selectArray.GetSize() ; i++ )
		{
			if( pEntity == (CEntity*)m_selectArray[i] )
				return TRUE ;
		}
	}
	return FALSE;
}

void CMyPaintDoc::DeleteContents() 
{
	m_selectArray.RemoveAll();

	// 清除图元链表中的图元对象
	POSITION	pos = m_EntityList.GetHeadPosition();
	while(pos!=NULL)
	{
		CEntity*	pEntity = (CEntity *) m_EntityList.GetNext(pos);
		delete pEntity ;
	}

	m_EntityList.RemoveAll() ;
	
	CDocument::DeleteContents();
}

⌨️ 快捷键说明

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