📄 drawdoc.cpp
字号:
// DrawDoc.cpp : implementation of the CDrawDoc class
//
#include "stdafx.h"
#include "SwitchViewMDI.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_WINDOW_NEWVIEW, OnWindowNewView)
ON_COMMAND(ID_WINDOW_NEWFORMVIEW, OnWindowNewFormView)
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 );
}
///////////////////////////////////////////////////////////
// Create a new view based on the pTemplate document
// template and associated with pDocument
CFrameWnd* EkCreateNewWindow( CDocTemplate* pTemplate,
CDocument* pDocument )
{
ASSERT_VALID( pTemplate );
ASSERT_VALID( pDocument );
// 1 - Create the new frame window
// (will in turn create the associated view)
CFrameWnd* pFrame = pTemplate->CreateNewFrame(
pDocument, NULL );
if( pFrame == NULL )
{
// Window creation failed
TRACE0( "Warning: failed to create new frame.\n" );
return NULL;
}
ASSERT_KINDOF( CFrameWnd, pFrame );
// 2 - Tell the frame to update itself
// (and its child windows)
pTemplate->InitialUpdateFrame( pFrame, pDocument );
// 3 - Return a pointer to the new frame window object
return pFrame;
}
void CDrawDoc::OnWindowNewView()
{
EkCreateNewWindow( theApp.m_ptDrawView, this );
}
void CDrawDoc::OnWindowNewFormView()
{
EkCreateNewWindow( theApp.m_ptDrawFormView, this );
}
///////////////////////////////////////////////////////////
// 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 + -