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

📄 mainfrm.cpp

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

#include "stdafx.h"
#include "PositionToolbars.h"

#include "MainFrm.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_VIEW_SHAPES_TOOLBAR, OnViewShapesToolbar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_SHAPES_TOOLBAR, OnUpdateViewShapesToolbar)

	ON_COMMAND_EX(ID_COLOR_RED, OnColor)
	ON_COMMAND_EX(ID_COLOR_GREEN, OnColor)
	ON_COMMAND_EX(ID_COLOR_BLUE, OnColor)
	ON_UPDATE_COMMAND_UI(ID_COLOR_RED, OnUpdateColor)
	ON_UPDATE_COMMAND_UI(ID_COLOR_GREEN, OnUpdateColor)
	ON_UPDATE_COMMAND_UI(ID_COLOR_BLUE, OnUpdateColor)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

///////////////////////////////////////////////////////////
// EkIsBarVisible

BOOL EkIsBarVisible( CControlBar* pBar )
{
	ASSERT_VALID( pBar );

	return ( ( pBar->GetStyle() & WS_VISIBLE ) != 0 );
}

///////////////////////////////////////////////////////////
// EkDockBarNextTo

void EkDockBarNextTo(	CControlBar* pNewBar,
						CControlBar* pDockedBar,
						UINT nDockBarID = AFX_IDW_DOCKBAR_TOP )
{
	ASSERT_VALID( pDockedBar );
	ASSERT_VALID( pNewBar );

	// 1 - Find the frame where we will dock
	CFrameWnd* pFrame = pDockedBar->GetDockingFrame();

	// 2 - Force MFC to compute the positions
	// of the docked control bar(s)
	pFrame->RecalcLayout();

	// 3 - Compute rectangle of already "docked" bar
	CRect rect;
	pDockedBar->GetWindowRect( &rect );

	// 4 - Offset the rectangle slightly to the bottom right
	// so that the new bar will dock either to the right or
	// to the bottom of the existing bar (depending on the
	// side where this last bar is already docked)
	rect.OffsetRect(1,1);

	// 5 - Dock new bar to specified position
	pFrame->DockControlBar( pNewBar, nDockBarID, &rect );
}

///////////////////////////////////////////////////////////
// EkCreateToolBar

BOOL EkCreateToolBar(	CFrameWnd* pParentFrame,
						CToolBar* pBar,
						UINT nIDBar,
						UINT nIDResource = 0,
						DWORD dwStyle1 = WS_CHILD | WS_VISIBLE | CBRS_TOP,
						DWORD dwStyle2 = CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC )
{
	ASSERT_VALID( pParentFrame );
	ASSERT_VALID( pBar );

	// 1 - No IDResource means resource ID is same as bar ID
	if( nIDResource == 0 )
	{
		nIDResource = nIDBar;
	}

	// 2 - Create toolbar and load resources
	if( !pBar->Create( pParentFrame, dwStyle1, nIDBar ) ||
		!pBar->LoadToolBar( nIDResource ) )
	{
		TRACE1( "Failed to create toolbar with ID=%d\n", nIDBar );
		return FALSE;      // fail to create
	}

	// 3 - Define toolbar styles
	pBar->SetBarStyle( pBar->GetBarStyle() | dwStyle2 );

	return TRUE;
}

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

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	m_nColor = ID_COLOR_RED;	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if(		!EkCreateToolBar( this, &m_wndToolBar, AFX_IDW_TOOLBAR, IDR_MAINFRAME )
		||	!EkCreateToolBar( this, &m_wndTBshapes, IDR_SHAPES )
		||	!EkCreateToolBar( this, &m_wndTBdemo, IDR_DEMO_TOOLBAR )
		||	!EkCreateToolBar( this, &m_wndTBcolors1, IDR_COLORS )
		||	!EkCreateToolBar( this, &m_wndTBcolors2, IDR_COLORS ) )
	{
		return -1;	// fail to create
	}

	m_wndTBshapes.SetWindowText( CString( LPCSTR( IDR_SHAPES ) ) );

	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: top band, left position
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar( &m_wndToolBar, AFX_IDW_DOCKBAR_TOP );

	// Shapes toolbar docking: top band, next to standard toolbar
	m_wndTBshapes.EnableDocking(CBRS_ALIGN_ANY);
	EkDockBarNextTo( &m_wndTBshapes, &m_wndToolBar, AFX_IDW_DOCKBAR_TOP );

	// Colors1 toolbar docking: top band, next to shapes toolbar
	m_wndTBcolors1.EnableDocking(CBRS_ALIGN_ANY);
	EkDockBarNextTo( &m_wndTBcolors1, &m_wndTBshapes, AFX_IDW_DOCKBAR_TOP );

	// Demo toolbar docking: left band
	m_wndTBdemo.EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar( &m_wndTBdemo, AFX_IDW_DOCKBAR_LEFT );

	// Colors2 toolbar docking: left band, next to demo toolbar
	m_wndTBcolors2.EnableDocking(CBRS_ALIGN_ANY);
	EkDockBarNextTo( &m_wndTBcolors2, &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

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

void CMainFrame::OnViewShapesToolbar() 
{
	ShowControlBar( &m_wndTBshapes, !EkIsBarVisible( &m_wndTBshapes ), FALSE );
}

void CMainFrame::OnUpdateViewShapesToolbar(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck( EkIsBarVisible( &m_wndTBshapes ) );	
}

BOOL CMainFrame::OnColor(UINT nID)
{
	m_nColor = nID;
	return TRUE;
}

void CMainFrame::OnUpdateColor(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck( m_nColor == pCmdUI->m_nID );	
}

⌨️ 快捷键说明

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