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

📄 drawdoc.cpp

📁 是MFCfaq的一本书的源代码
💻 CPP
字号:
// DrawDoc.cpp : implementation of the CDrawDoc class
//

#include "stdafx.h"
#include "SwitchViewSDI.h"

#include "DrawDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDrawDoc

IMPLEMENT_DYNCREATE(CDrawDoc, CDocument)

BEGIN_MESSAGE_MAP(CDrawDoc, CDocument)
	//{{AFX_MSG_MAP(CDrawDoc)
	ON_COMMAND(ID_DRAW_SQUARE, OnDrawSquare)
	ON_COMMAND(ID_DRAW_CIRCLE, OnDrawCircle)
	ON_UPDATE_COMMAND_UI(ID_DRAW_SQUARE, OnUpdateDrawSquare)
	ON_UPDATE_COMMAND_UI(ID_DRAW_CIRCLE, OnUpdateDrawCircle)
	ON_COMMAND(ID_SWITCH_DRAWVIEW, OnSwitchDrawView)
	ON_COMMAND(ID_SWITCH_FORMVIEW, OnSwitchFormView)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDrawDoc construction/destruction

CDrawDoc::CDrawDoc()
{
	m_point = CPoint( 30, 30 );
	m_shape = SQUARE;
}

CDrawDoc::~CDrawDoc()
{
}

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

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

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CDrawDoc serialization

void CDrawDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar << m_point << (DWORD) m_shape;
	}
	else
	{
		ar >> m_point >> (DWORD&) m_shape;
	}
}

/////////////////////////////////////////////////////////////////////////////
// CDrawDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CDrawDoc commands

void CDrawDoc::OnDrawSquare() 
{
	m_shape = SQUARE;
	UpdateAllViews( NULL );
	SetModifiedFlag( TRUE );
}

void CDrawDoc::OnDrawCircle() 
{
	m_shape = CIRCLE;
	UpdateAllViews( NULL );
	SetModifiedFlag( TRUE );
}

void CDrawDoc::OnUpdateDrawSquare(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck( m_shape == SQUARE );
}

void CDrawDoc::OnUpdateDrawCircle(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck( m_shape == CIRCLE );
}

///////////////////////////////////////////////////////////
// Replace the current view in the frame window pFrame
// by a new view of class pViewClass

void EkSwitchViewInFrame(	CFrameWnd* pFrame,
							CRuntimeClass* pViewClass )
{
	ASSERT_VALID( pFrame );
	ASSERT( pViewClass != NULL );

	ASSERT( pViewClass->
				IsDerivedFrom( RUNTIME_CLASS( CView ) ) );

	// 1 - Find the currently active view
	CView* pActiveView = pFrame->GetActiveView();
	if( pActiveView == NULL )
	{
		TRACE0( "Unable to switch: no active view\n" );
		return;
	}

	if( pActiveView->IsKindOf( pViewClass ) )
	{
		// No need to switch for same view class
		return;
	}

	// 2 - Store current view position
	CRect rcView;
	pActiveView->GetWindowRect( &rcView );

	// 3 - Find the associated document
	CDocument* pDoc = pActiveView->GetDocument();
	ASSERT_VALID( pDoc );

	// 4 - Make sure the document won't self-destruct
	// when active view is destroyed
	BOOL bSaveAutoDelete = pDoc->m_bAutoDelete;
	pDoc->m_bAutoDelete = FALSE;

	// 5 - Destroy the active view
	pActiveView->DestroyWindow();

	// 6 - Restore document to initial state
	pDoc->m_bAutoDelete = bSaveAutoDelete;

	// 7 - Initialize creation context used by CreateView
	CCreateContext context;
	context.m_pNewDocTemplate = NULL;
	context.m_pLastView = NULL;
	context.m_pCurrentFrame = NULL;

	context.m_pNewViewClass = pViewClass;
	context.m_pCurrentDoc = pDoc;

	// 8 - Create the new view
	CView* pNewView = static_cast<CView*>
						( pFrame->CreateView( &context ) );

	ASSERT_VALID( pNewView );

	// 9 - Position the new view like the old one
	pFrame->ScreenToClient( &rcView );
	pNewView->MoveWindow( &rcView, TRUE );
	
	// 10 - Send WM_INITIALUPDATE to the view
	pFrame->InitialUpdateFrame( pDoc, TRUE );
}

///////////////////////////////////////////////////////////
// Return the active frame window or NULL

CFrameWnd* EkGetActiveFrame()
{
	CWnd* pMainWnd = AfxGetMainWnd();

	ASSERT_VALID( pMainWnd );

	if( !pMainWnd->IsKindOf( RUNTIME_CLASS( CFrameWnd ) ) )
	{
		return NULL;
	}

	return static_cast<CFrameWnd*>( pMainWnd )
										->GetActiveFrame();
}

///////////////////////////////////////////////////////////

#include "DrawView.h"
#include "DrawFormView.h"

void CDrawDoc::OnSwitchDrawView() 
{
	CFrameWnd* pActiveFrame = EkGetActiveFrame();

	if( pActiveFrame )
	{
		EkSwitchViewInFrame(	pActiveFrame,
								RUNTIME_CLASS( CDrawView ) );
	}
}

void CDrawDoc::OnSwitchFormView() 
{
	CFrameWnd* pActiveFrame = EkGetActiveFrame();

	if( pActiveFrame )
	{
		EkSwitchViewInFrame(	pActiveFrame,
								RUNTIME_CLASS( CDrawFormView ) );
	}
}

⌨️ 快捷键说明

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