myobjcommand.cpp

来自「实现撤销和恢复功能得远吗」· C++ 代码 · 共 72 行

CPP
72
字号
// MyObjCommand.cpp: implementation of the CMyObjCommand class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TestUndo.h"
#include "MyObjCommand.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMyObjCommand::CMyObjCommand(CTestUndoDoc * pDoc, int nOperID,CMyObject * pObj,CRect rect)
{
	m_pDoc    = pDoc;
	m_nOperID = nOperID;
	m_rect    = rect;
	m_pObj    = pObj;

	ASSERT(m_pDoc);
}

CMyObjCommand::~CMyObjCommand()
{
	if( m_nOperID == 0 )
	{
		m_pObj->Release( );
	}
}

bool CMyObjCommand::Execute( )
{
	if( m_nOperID == 0 )   // 增加对象
	{
		m_pObj = new CMyObject( m_rect );
		m_pObj->AddRef( );
		m_pDoc->m_objects.Add( m_pObj );
	}
	else if( m_nOperID == 1 )   // 更改属性
	{
		ASSERT(m_pObj);
		CRect rect = m_pObj->GetObjRect();
		m_pObj->SetObjRect( m_rect );
		m_rect = rect;
	}

	return true;
}

bool CMyObjCommand::Unexecute( )
{
	if( m_nOperID == 0 )
	{
		m_pDoc->m_objects.Remove( m_pObj );
	}
	else if( m_nOperID == 1 )
	{
		ASSERT(m_pObj);
		CRect rect = m_pObj->GetObjRect();
		m_pObj->SetObjRect(m_rect);
		m_rect = rect;
	}

	return true;
}

⌨️ 快捷键说明

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