📄 mvdoctemplate.cpp
字号:
CloseAllDocuments( FALSE );
return NULL;
}
// Set the document file path and name.
pDocument->SetPathName( _lpszPathName );
}
// Now that the document is created, we can create all the associated frame.
CDocFrameMgr* pDocFrameMgr = new CDocFrameMgr( pDocument, m_FrameTemplateList, m_pFrameToActivate );
if ( !pDocFrameMgr )
{
// Error creating the object.
TRACE( "Unable to create the associated frame in OpenDocumentFile \
in %s at %d.\n", THIS_FILE, __LINE__ );
// We must delete the document instance.
RemoveDocument( pDocument );
delete pDocument;
CloseAllDocuments( FALSE );
return NULL;
}
// Init the frame.
pDocFrameMgr->OpenStartupFrame();
m_DocumentFrameList.AddTail( pDocFrameMgr );
// Return the created document.
return pDocument;
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// We overload the remove document to remove the associated memory with that
// document. We must delete the associated docframemanager.
void CMvDocTemplate::CleanDocument( CDocument* _pDoc )
{
// Validate the document.
if ( !_pDoc )
{
ASSERT ( _pDoc );
TRACE( "Invalid document parameter in RemoveDocument in \
in %s at %d.\n", THIS_FILE, __LINE__ );
return;
}
// Make sure the document is valid.
ASSERT_VALID( _pDoc );
// Must delete the associated frame manager.
CDocFrameMgr* pDocFrameMgr = GetAssociatedDocFrameMgr( _pDoc );
m_DocumentFrameList.RemoveAt( m_DocumentFrameList.Find( pDocFrameMgr ) );
delete pDocFrameMgr;
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// To add a specific frame template to that kind of document.
void CMvDocTemplate::AddFrameTemplate(
CFrameTemplate* _pFrameTemplate,
bool _bActivate /* = false */ )
{
// Validate the needed information.
if ( !_pFrameTemplate )
{
// Must never happen.
ASSERT ( _pFrameTemplate );
TRACE( "Invalid frame template specify is the AddFrameTemplate in \
in %s at %d.\n", THIS_FILE, __LINE__ );
return;
}
// Add the frame template in the map index.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
m_FrameTemplateList.AddTail( _pFrameTemplate );
// Remember the frame template if it's the one to activate.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
if ( _bActivate )
{
m_pFrameToActivate = _pFrameTemplate;
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Register the associated icon with the document. Actually fix a
// problem with the way MS register your document icon in the system.
void CMvDocTemplate::RegisterIconType( void )
{
// Get the application name.
char buffer[1024];
GetModuleFileName( NULL, buffer, 1024 );
CString sBuffer( buffer );
// Set the icon key to the resource id value.
CString Icon = sBuffer + ",-";
Icon.Format( Icon + "%d", m_nIDResource );
// Get the doc string.
CString RegDocName;
GetDocString( RegDocName, CDocTemplate::regFileTypeId) ;
RegSetValue( HKEY_CLASSES_ROOT, RegDocName + "\\DefaultIcon", REG_SZ, Icon, lstrlen( Icon ) * sizeof( TCHAR ) );
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Overloaded method to manage the list of frame.
CFrameWnd* CMvDocTemplate::CreateNewFrame(
CDocument* _pDoc,
CFrameWnd* _pOther )
{
// Find the correspondant template, and validate it.
CDocFrameMgr* pDocFrameMgr = GetAssociatedDocFrameMgr( _pDoc );
CFrame* pChosenFrame = pDocFrameMgr->GetActiveFrame();
if ( !pChosenFrame )
{
// Not found.
// Not supposed to happen.
ASSERT( pChosenFrame );
return NULL;
}
// Create the associated window and return it.
if ( AfxGetMainWnd() && ( ( CMDIFrameWnd* ) AfxGetMainWnd() )->GetActiveFrame() )
{
CDocument* pDoc = ( ( CMDIFrameWnd* ) AfxGetMainWnd() )->GetActiveFrame()->GetActiveDocument();
CMDIChildWnd* pNewFrameWnd = ( CMDIChildWnd* ) ( pChosenFrame->CreateNewFrame( pDoc, NULL ) );
return pNewFrameWnd;
}
return NULL;
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Microsoft is using the first icon in your exe file as you doc icon.
// If your icon is not the first one, then you are stuck. Use that method
// instead and it'll fix your problem.
void CMvDocTemplate::InitialUpdateFrame(
CFrameWnd* _pFrameWnd,
CDocument* _pDoc,
BOOL _bMakeVisible /* = TRUE */ )
{
// Validate the needed information.
if ( !_pFrameWnd )
{
// Must never happen.
ASSERT( _pFrameWnd );
TRACE( "Invalid frame argument in InitialUpdateFrame.\n", THIS_FILE, __LINE__ );
return;
}
// Make sure that the frame is a mdi child window.
if ( !_pFrameWnd->IsKindOf( RUNTIME_CLASS( CMDIChildWnd ) ) )
{
// Must never happen.
ASSERT( _pFrameWnd->IsKindOf( RUNTIME_CLASS( CMDIChildWnd ) ) );
TRACE( "Invalid MDI frame in InitialUpdateFrame.\n", THIS_FILE, __LINE__ );
return;
}
// Initialize the newly created frame.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
_pFrameWnd->InitialUpdateFrame( _pDoc, _bMakeVisible );
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Overloaded method to open the frame associated with the event id.
BOOL CMvDocTemplate::OnCmdMsg(
UINT _nID, int _nCode, void* _pExtra,
AFX_CMDHANDLERINFO* _pHandlerInfo )
{
// If pHandlerInfo is NULL, then handle the message.
if ( _pHandlerInfo == NULL )
{
// We must get the active document.
CDocument* pDoc = NULL;
if ( AfxGetMainWnd() && ( ( CMDIFrameWnd* ) AfxGetMainWnd() )->GetActiveFrame() )
{
pDoc = ( ( CMDIFrameWnd* ) AfxGetMainWnd() )->GetActiveFrame()->GetActiveDocument();
}
// Retrieve the associate doc manager and validate it.
BOOL bProcessed = FALSE;
CDocFrameMgr* pDocFrameMgr = GetAssociatedDocFrameMgr( pDoc );
if ( pDocFrameMgr )
{
bProcessed = pDocFrameMgr->OnCmdMsg( _nID, _nCode, _pExtra );
}
// Make sure it was processed.
if ( bProcessed )
{
return TRUE;
}
}
// The message was not for us.
// Call the parent class method.
return CMultiDocTemplate::OnCmdMsg( _nID, _nCode, _pExtra, _pHandlerInfo );
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// To find the frame template associated to the given class and frame.
CFrame* CDocFrameMgr::FindFrameEx(
CString _ViewClassName,
CString _FrameClassName )
{
// Scan the entire list to find the associated frame.
POSITION Position = m_FrameList.GetHeadPosition();
while ( Position )
{
CFrame* pFrame = m_FrameList.GetNext( Position );
// Validate the frame template.
if ( !pFrame )
{
// Must never happen.
ASSERT ( pFrame );
TRACE ( "Invalid frame in FindFrameTemplate \
in %s at %d.\n", THIS_FILE, __LINE__ );
}
else if ( pFrame->GetViewRTC()->m_lpszClassName == _ViewClassName &&
pFrame->GetFrameRTC()->m_lpszClassName == _FrameClassName )
{
// Found it.
return pFrame;
}
}
// Not found.
return NULL;
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This constructor build all the required frame based on the frame template
// list. Also store the frame to activate for later use.
CDocFrameMgr::CDocFrameMgr(
CDocument* _pDocument,
FrameTemplateList_& _FrameTemplateList,
CFrameTemplate* _pFrameToActivate )
{
// Keep the document pointer for later use.
m_pDocument = _pDocument;
m_pFrameToActivate = NULL;
// Create the frame template for each frame info in the list.
POSITION pos = _FrameTemplateList.GetHeadPosition( );
while ( pos )
{
CFrameTemplate* pFrameTemplate = _FrameTemplateList.GetNext( pos );
CFrame* pFrame = new CFrame( pFrameTemplate, m_pDocument );
m_FrameList.AddTail( pFrame );
if ( pFrameTemplate == _pFrameToActivate )
{
m_pFrameToActivate = pFrame;
}
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Free the allocated memory in the frame list.
CDocFrameMgr::~CDocFrameMgr( void )
{
while ( m_FrameList.GetCount() )
{
delete m_FrameList.RemoveTail();
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Usefull method to interact with the different frame.
bool CDocFrameMgr::OpenStartupFrame( void )
{
// Local definition.
CFrameWnd* WndToActivate = NULL;
// Scan the map and open the frame that must be open
// at startup.
POSITION Position = m_FrameList.GetHeadPosition();
while ( Position )
{
CFrame* pFrame = m_FrameList.GetNext( Position );
if ( pFrame->GetLoadAtStartup() )
{
// The frame must be loaded when the document is being open.
// Create the frame instance and validate it.
CFrameWnd* pFrameWnd = pFrame->CreateFrame();
if ( pFrameWnd )
{
// The frame is now created, we must activate it.
pFrameWnd->ShowWindow( pFrame->GetDefaultWndStatus() );
if ( ( m_pFrameToActivate == pFrame ) ||
!m_pFrameToActivate )
{
WndToActivate = pFrameWnd;
}
}
}
}
// Activate the window.
if ( WndToActivate )
{
WndToActivate->ActivateFrame();
WndToActivate->SetFocus();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -