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

📄 mainfrm.cpp

📁 这是MFC经典问答书的光盘内容
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "TwoViews.h"

#include "MainFrm.h"

#include "DrawDoc.h"
#include "CountDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_WINDOW_NEW_DRAWING1, OnWindowNewDrawing1)
	ON_COMMAND(ID_WINDOW_NEW_DRAWING2, OnWindowNewDrawing2)
	ON_COMMAND(ID_WINDOW_NEW_COUNTER1, OnWindowNewCounter1)
	ON_COMMAND(ID_WINDOW_NEW_COUNTER2, OnWindowNewCounter2)
	ON_UPDATE_COMMAND_UI(ID_WINDOW_NEW_DRAWING1, OnUpdateWindowNewDrawing)
	ON_UPDATE_COMMAND_UI(ID_WINDOW_NEW_DRAWING2, OnUpdateWindowNewDrawing)
	ON_UPDATE_COMMAND_UI(ID_WINDOW_NEW_COUNTER1, OnUpdateWindowNewCounter)
	ON_UPDATE_COMMAND_UI(ID_WINDOW_NEW_COUNTER2, OnUpdateWindowNewCounter)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.Create(this) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create standard toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndTBdemo.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP, IDR_DEMO_TOOLBAR) ||
		!m_wndTBdemo.LoadToolBar(IDR_DEMO_TOOLBAR))
	{
		TRACE0("Failed to create demo toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// Mainframe docking
	EnableDocking(CBRS_ALIGN_ANY);

	// Standard toolbar docking
	m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
					CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	// Demo toolbar docking
	m_wndTBdemo.SetBarStyle(m_wndTBdemo.GetBarStyle() |
					CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
	m_wndTBdemo.EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndTBdemo, AFX_IDW_DOCKBAR_LEFT);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CMDIFrameWnd::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG


///////////////////////////////////////////////////////////
// Return a pointer to the active document object or NULL

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();
}

///////////////////////////////////////////////////////////
// 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;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

void CMainFrame::OnWindowNewDrawing1() 
{
	CDocument* pDoc = EkGetActiveDocument();
	if( pDoc == NULL )
		return;

	EkCreateNewWindow( theApp.m_ptDrawing1,
					   pDoc );
}

void CMainFrame::OnWindowNewDrawing2() 
{
	CDocument* pDoc = EkGetActiveDocument();
	if( pDoc == NULL )
		return;

	EkCreateNewWindow( theApp.m_ptDrawing2,
					   pDoc );
}

void CMainFrame::OnWindowNewCounter1() 
{
	CDocument* pDoc = EkGetActiveDocument();
	if( pDoc == NULL )
		return;

	EkCreateNewWindow( theApp.m_ptCounter1,
					   pDoc );
}

void CMainFrame::OnWindowNewCounter2() 
{
	CDocument* pDoc = EkGetActiveDocument();
	if( pDoc == NULL )
		return;

	EkCreateNewWindow( theApp.m_ptCounter2,
					   pDoc );
}

void CMainFrame::OnUpdateWindowNewDrawing(CCmdUI* pCmdUI) 
{
	// Only enable the command if :
	//    1 - There *is* an active document
	//    2 - This document is of the CDrawDoc type
	BOOL bEnable = FALSE;	
	CDocument* pDoc = EkGetActiveDocument();
	if( pDoc != NULL )
	{
		bEnable = pDoc->IsKindOf( RUNTIME_CLASS( CDrawDoc ) );
	}

	pCmdUI->Enable( bEnable );
}

void CMainFrame::OnUpdateWindowNewCounter(CCmdUI* pCmdUI) 
{
	// Only enable the command if :
	//    1 - There *is* an active document
	//    2 - This document is of the CCountDoc type
	BOOL bEnable = FALSE;	
	CDocument* pDoc = EkGetActiveDocument();
	if( pDoc != NULL )
	{
		bEnable = pDoc->IsKindOf( RUNTIME_CLASS( CCountDoc ) );
	}

	pCmdUI->Enable( bEnable );
}

⌨️ 快捷键说明

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