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

📄 mainfrm.cpp

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

#include "stdafx.h"
#include "SwitchToolbars.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(ID_VIEW_ALTERNATE_TOOLBARS, OnViewAlternateToolbars)
	ON_UPDATE_COMMAND_UI(ID_VIEW_ALTERNATE_TOOLBARS, OnUpdateViewAlternateToolbars)
	//}}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;
}

///////////////////////////////////////////////////////////
// EkDockToolBar

void EkDockToolBar(	CFrameWnd* pParentFrame,
					CToolBar* pBar,
					UINT nDockBarID = AFX_IDW_DOCKBAR_TOP,
					CControlBar* pBarNextTo = NULL,
					DWORD dwStyleDocking = CBRS_ALIGN_ANY )
{
	ASSERT_VALID( pParentFrame );
	ASSERT_VALID( pBar );

	ASSERT( ::IsWindow( pBar->GetSafeHwnd() ) );

	// 1 - Define toolbar docking behavior
	pBar->EnableDocking( dwStyleDocking );

	if( pBarNextTo != NULL )
	{
		// 2a - Dock toolbar next to an existing bar
		EkDockBarNextTo( pBar, pBarNextTo, nDockBarID );
	}
	else
	{
		// 2b - Dock toolbar on its own dockbar
		pParentFrame->DockControlBar( pBar, nDockBarID );
	}
}

///////////////////////////////////////////////////////////
// EkSetToolBarButtonText

void EkSetToolBarButtonText( CToolBar* pBar, UINT nIDStrings, TCHAR chSep = _T( '\n' ) )
{
	ASSERT_VALID( pBar );
	
	// 1 - Load string from resource
	CString strButtons;
	VERIFY( strButtons.LoadString( nIDStrings ) );

	// 2 - Start at beginning of string
	int nIndex = 0;
	LPCTSTR pszStart = strButtons;
	LPCTSTR pszEnd = NULL;

	// 3 - Isolate each substring and set button text
	do
	{
		// 4 - Find next separator
		pszEnd = _tcschr( pszStart, chSep );

		int nLen = ( pszEnd == NULL ) ?
						_tcslen( pszStart ) : (int)( pszEnd - pszStart );
		ASSERT( nLen >= 0 );

		// 5 - Create CString from substring
		CString strSubString;
		memcpy(	strSubString.GetBufferSetLength( nLen ),
				pszStart, nLen * sizeof( TCHAR ) );

		// 6 - Set button text, bump index
		pBar->SetButtonText( nIndex++, strSubString );

		// 7 - Skip separator
		pszStart += nLen + 1;
	}
	while( pszEnd != NULL );
}

///////////////////////////////////////////////////////////
// EkSwitchBars

void EkSwitchBars(	CFrameWnd* pFrame,
					CControlBar* pBar1,
					CControlBar* pBar2,
					BOOL bShowBar2,
					BOOL bDelay = TRUE )
{
	ASSERT_VALID( pFrame );
	ASSERT_VALID( pBar1 );
	ASSERT_VALID( pBar2 );
	
	// 1 - Compute "From" and "To" bars
	CControlBar* pBarFrom = bShowBar2 ? pBar1 : pBar2;
	CControlBar* pBarTo = bShowBar2 ? pBar2 : pBar1;
	BOOL bVisible = EkIsBarVisible( pBarFrom );

	// 2 - Exchange "From" and "To" window IDs
	UINT nIDFrom = ::GetWindowLong( pBarFrom->GetSafeHwnd(), GWL_ID );
	UINT nIDTo = ::GetWindowLong( pBarTo->GetSafeHwnd(), GWL_ID );

	::SetWindowLong( pBarFrom->GetSafeHwnd(), GWL_ID, nIDTo );
	::SetWindowLong( pBarTo->GetSafeHwnd(), GWL_ID, nIDFrom );

	// 3 - Hide "From" bar, show "To" bar if "From" bar was visible
	pFrame->ShowControlBar( pBarFrom, FALSE, bDelay );
	pFrame->ShowControlBar( pBarTo, bVisible, bDelay );
}

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

CMainFrame::CMainFrame()
{
	m_bAlternateToolBars = FALSE;	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// Create standard toolbars
	if(		!EkCreateToolBar( this, &m_wndToolBar, AFX_IDW_TOOLBAR, IDR_MAINFRAME )
		||	!EkCreateToolBar( this, &m_wndTBshapes, IDR_SHAPES )
		||	!EkCreateToolBar( this, &m_wndTBdemo, IDR_DEMO_TOOLBAR ) )
	{
		return -1;
	}

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

	// Create alternate toolbars
	if(		!EkCreateToolBar( this, &m_wndToolBar2, IDR_MAINFRAME2 )
		||	!EkCreateToolBar( this, &m_wndTBshapes2, IDR_SHAPES2 ) )
	{
		return -1;
	}

	EkSetToolBarButtonText(	&m_wndToolBar2, IDS_BUTTONS_STANDARD );
	m_wndToolBar2.SetSizes( CSize( 48, 35 ), CSize( 16, 15 ) );

	EkSetToolBarButtonText(	&m_wndTBshapes2, IDS_BUTTONS_SHAPES );
	m_wndTBshapes2.SetSizes( CSize( 48, 35 ), CSize( 16, 15 ) );

	m_wndTBshapes2.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);

	// Toolbar docking
	PositionToolBars( FALSE );
	PositionToolBars( TRUE );
	EkDockToolBar( this, &m_wndTBdemo, AFX_IDW_DOCKBAR_LEFT );

	// Initially hide alternate toolbars
	ShowControlBar( &m_wndToolBar2, FALSE, TRUE );
	ShowControlBar( &m_wndTBshapes2, FALSE, TRUE );

	return 0;
}

void CMainFrame::PositionToolBars( BOOL bAlternate )
{
	if( !bAlternate )
	{
		// Lay out "standard" toolbars
		EkDockToolBar( this, &m_wndToolBar );
		EkDockToolBar( this, &m_wndTBshapes, AFX_IDW_DOCKBAR_TOP, &m_wndToolBar );
	}
	else
	{
		// Lay out "alternate" toolbars
		EkDockToolBar( this, &m_wndToolBar2 );
		EkDockToolBar( this, &m_wndTBshapes2, AFX_IDW_DOCKBAR_TOP, &m_wndToolBar2 );
	}
}

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() 
{
	CControlBar* pBar = GetControlBar( IDR_SHAPES );
	if (pBar != NULL)
	{
		ShowControlBar( pBar, !EkIsBarVisible( pBar ), FALSE);
	}
}

void CMainFrame::OnUpdateViewShapesToolbar(CCmdUI* pCmdUI) 
{
	CControlBar* pBar = GetControlBar( IDR_SHAPES );
	if (pBar != NULL)
	{
		pCmdUI->SetCheck( EkIsBarVisible( pBar ) );
	}
}

void CMainFrame::OnViewAlternateToolbars() 
{
	m_bAlternateToolBars = !m_bAlternateToolBars;

	EkSwitchBars( this, &m_wndToolBar, &m_wndToolBar2, m_bAlternateToolBars );
	EkSwitchBars( this, &m_wndTBshapes, &m_wndTBshapes2, m_bAlternateToolBars );

	// Reposition toolbars
	PositionToolBars( m_bAlternateToolBars );
}

void CMainFrame::OnUpdateViewAlternateToolbars(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck( m_bAlternateToolBars );	
}

⌨️ 快捷键说明

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