📄 ekutil.cpp
字号:
// EkUtil.cpp: implementation file
//
#include "stdafx.h"
#include "EkUtil.h"
//=======================================================//
// Chapter 02: Documents and Document Templates //
//=======================================================//
///////////////////////////////////////////////////////////
// CEkDocList: class implementation
CEkDocList::CEkDocList()
{
CWinApp* pApp = AfxGetApp();
ASSERT_VALID( pApp );
// 1 - Iterate through the application's document
// templates list
POSITION posTemplate =
pApp->GetFirstDocTemplatePosition();
while( posTemplate != NULL )
{
// 2 - For each document template object...
CDocTemplate* pTemplate =
pApp->GetNextDocTemplate( posTemplate );
ASSERT_VALID( pTemplate );
ASSERT_KINDOF( CDocTemplate, pTemplate );
// 3 - Iterate through the template's document list
POSITION posDocument =
pTemplate->GetFirstDocPosition();
while( posDocument != NULL )
{
// 4 - For each document object...
CDocument* pDoc =
pTemplate->GetNextDoc( posDocument );
ASSERT_VALID( pDoc );
ASSERT_KINDOF( CDocument, pDoc );
// 5 - Add the document pointer to our
// internal list
m_DocList.AddTail( pDoc );
}
}
// 6 - Initialize our POSITION member
m_posDocList = m_DocList.GetHeadPosition();
}
CEkDocList::~CEkDocList()
{
m_DocList.RemoveAll();
m_posDocList = NULL;
}
CDocument* CEkDocList::GetNextDoc()
{
if( m_posDocList == NULL )
return NULL;
CDocument* pDoc = m_DocList.GetNext( m_posDocList );
ASSERT_VALID( pDoc );
ASSERT_KINDOF( CDocument, pDoc );
return pDoc;
}
///////////////////////////////////////////////////////////
// EkCloseDocument: function implementation
void EkCloseDocument( CDocument* pDoc )
{
ASSERT_VALID( pDoc );
// 1 - Give the user a chance to save his document
if( !pDoc->SaveModified() )
return;
// 2 - Destroy the document object
pDoc->OnCloseDocument();
}
///////////////////////////////////////////////////////////
// EkGetActiveDocument: function implementation
CDocument* EkGetActiveDocument()
{
// 1 - Get a pointer to the application's
// main frame window
CWnd* pWnd = AfxGetMainWnd();
if( pWnd == NULL )
return NULL;
// 2 - Make sure the pointer is valid and more
// strongly typed
ASSERT_VALID( pWnd );
ASSERT_KINDOF( CFrameWnd, pWnd );
CFrameWnd* pMainFrame = static_cast< CFrameWnd* >( pWnd );
// 3 - Get a pointer to the active frame window
// (may be 'this' for SDI application)
CFrameWnd* pActiveFrame = pMainFrame->GetActiveFrame();
if( pActiveFrame == NULL )
return NULL;
// 4 - Return a pointer to the active document object
return pActiveFrame->GetActiveDocument();
}
//=======================================================//
// Chapter 03: Views and Frame Windows //
//=======================================================//
///////////////////////////////////////////////////////////
// CEkFixedFormFrame: class implementation
IMPLEMENT_DYNCREATE(CEkFixedFormFrame, CMDIChildWnd)
CEkFixedFormFrame::CEkFixedFormFrame()
{
}
CEkFixedFormFrame::~CEkFixedFormFrame()
{
}
BEGIN_MESSAGE_MAP(CEkFixedFormFrame, CMDIChildWnd)
//{{AFX_MSG_MAP(CEkFixedFormFrame)
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CEkFixedFormFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// Make the frame window non-resizable
cs.style &= ~WS_THICKFRAME;
cs.style &= ~WS_MAXIMIZEBOX;
return CMDIChildWnd::PreCreateWindow(cs);
}
int CEkFixedFormFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// The child FormView should already have been created
// by the call to CMDIChildWnd::OnCreate()
CFormView* pFormView = static_cast< CFormView* >
( GetDescendantWindow( AFX_IDW_PANE_FIRST, TRUE ) );
ASSERT_VALID( pFormView );
ASSERT_KINDOF( CFormView, pFormView );
// Make sure that the view window size is correctly set
RecalcLayout();
// Make the view resize its parent frame -- that is
// "us", normally !
pFormView->ResizeParentToFit( FALSE );
pFormView->ResizeParentToFit( TRUE );
return 0;
}
///////////////////////////////////////////////////////////
// CEkLockableSplitter: class implementation
CEkLockableSplitter::CEkLockableSplitter()
{
m_bLocked = FALSE;
}
CEkLockableSplitter::~CEkLockableSplitter()
{
}
BEGIN_MESSAGE_MAP(CEkLockableSplitter, CSplitterWnd)
//{{AFX_MSG_MAP(CEkLockableSplitter)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CEkLockableSplitter::SetLock( BOOL bLock )
{
m_bLocked = bLock;
}
BOOL CEkLockableSplitter::GetLock() const
{
return m_bLocked;
}
void CEkLockableSplitter::ToggleLock()
{
m_bLocked = !m_bLocked;
}
void CEkLockableSplitter::OnLButtonDown(UINT nFlags, CPoint point)
{
if( m_bLocked )
{
// Bypass standard CSplitterWnd processing
CWnd::OnLButtonDown(nFlags, point);
}
else
{
CSplitterWnd::OnLButtonDown(nFlags, point);
}
}
void CEkLockableSplitter::OnMouseMove(UINT nFlags, CPoint point)
{
if( m_bLocked )
{
// Bypass standard CSplitterWnd processing
CWnd::OnMouseMove(nFlags, point);
}
else
{
CSplitterWnd::OnMouseMove(nFlags, point);
}
}
BOOL CEkLockableSplitter::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if( m_bLocked )
{
// Bypass standard CSplitterWnd processing
return CWnd::OnSetCursor(pWnd, nHitTest, message);
}
else
{
return CSplitterWnd::OnSetCursor(pWnd, nHitTest, message);
}
}
///////////////////////////////////////////////////////////
// EkCreateNewWindow: function implementation
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;
}
///////////////////////////////////////////////////////////
// EkGetActiveFrame: function implementation
CFrameWnd* EkGetActiveFrame()
{
CWnd* pMainWnd = AfxGetMainWnd();
ASSERT_VALID( pMainWnd );
if( !pMainWnd->IsKindOf( RUNTIME_CLASS( CFrameWnd ) ) )
{
return NULL;
}
return static_cast<CFrameWnd*>( pMainWnd )
->GetActiveFrame();
}
///////////////////////////////////////////////////////////
// EkGetActiveView: function implementation
CView* EkGetActiveView()
{
CWnd* pMainWnd = AfxGetMainWnd();
ASSERT_VALID( pMainWnd );
if( !pMainWnd->IsKindOf( RUNTIME_CLASS( CFrameWnd ) ) )
{
return NULL;
}
return static_cast<CFrameWnd*>( pMainWnd )
->GetActiveFrame()->GetActiveView();
}
///////////////////////////////////////////////////////////
// EkSwitchViewInFrame: function implementation
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 );
}
///////////////////////////////////////////////////////////
// EkSwitchViewInSplitter: function implementation
void EkSwitchViewInSplitter( CSplitterWnd* pSplitter,
int row, int col,
CRuntimeClass* pViewClass )
{
ASSERT_VALID( pSplitter );
ASSERT( pViewClass != NULL );
ASSERT( pViewClass->
IsDerivedFrom( RUNTIME_CLASS( CView ) ) );
// 1 - Find the view to be replaced
CWnd* pPaneWnd = pSplitter->GetPane( row, col );
if( !pPaneWnd->IsKindOf( RUNTIME_CLASS( CView ) ) )
{
TRACE2( "Unable to switch: pane (%d,%d) is not a view\n",
row, col );
return;
}
CView* pCurrentView = static_cast<CView*>( pPaneWnd );
ASSERT_VALID( pCurrentView );
ASSERT_KINDOF( CView, pCurrentView );
if( pCurrentView->IsKindOf( pViewClass ) )
{
// No need to switch for same view class
return;
}
// 2 - Store current view position and activation state
CRect rcView;
pCurrentView->GetWindowRect( &rcView );
CView* pActiveView = pSplitter->
GetParentFrame()->GetActiveView();
BOOL bSaveActive = ( pActiveView == NULL )
|| ( pActiveView == pCurrentView );
// 3 - Find the associated document
CDocument* pDoc = pCurrentView->GetDocument();
ASSERT_VALID( pDoc );
// 4 - Make sure the document won't self-destruct
// when current view is destroyed
BOOL bSaveAutoDelete = pDoc->m_bAutoDelete;
pDoc->m_bAutoDelete = FALSE;
// 5 - Destroy the current view
pCurrentView->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
pSplitter->CreateView( row, col, pViewClass,
rcView.Size(), &context );
CView* pNewView = static_cast<CView*>
( pSplitter->GetPane( row, col ) );
ASSERT_VALID( pNewView );
ASSERT_KINDOF( CView, pNewView );
// 9 - Position the new view like the old one and
// activate it if needed
pSplitter->ScreenToClient( &rcView );
pNewView->MoveWindow( &rcView, TRUE );
if( bSaveActive )
{
pSplitter->GetParentFrame()->SetActiveView( pNewView );
}
// 10 - Send WM_INITIALUPDATE to the view
pNewView->GetParentFrame()->InitialUpdateFrame( pDoc, TRUE );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -