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

📄 mainfrm.cpp

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

#include "stdafx.h"
#include "SelectToolbars.h"

#include "MainFrm.h"

#include <afxpriv.h>	// for class CDockBar

#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_WM_CONTEXTMENU()

	// Standard toolbar
	ON_COMMAND_EX(ID_VIEW_TOOLBAR, OnToggleBar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_TOOLBAR, OnUpdateBarMenu)

	// Shapes toolbar
	ON_COMMAND_EX(ID_VIEW_SHAPES_TOOLBAR, OnToggleBar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_SHAPES_TOOLBAR, OnUpdateBarMenu)

	// Demo toolbar
	ON_COMMAND_EX(ID_VIEW_DEMO_TOOLBAR, OnToggleBar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_DEMO_TOOLBAR, OnUpdateBarMenu)

	// Standard status bar
	ON_COMMAND_EX(ID_VIEW_STATUS_BAR, OnToggleBar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_STATUS_BAR, OnUpdateBarMenu)

	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

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

/////////////////////////////////////////////////////////////////////////////
// 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_wndTBshapes.Create(	this, WS_CHILD | WS_VISIBLE | CBRS_TOP,
								ID_VIEW_SHAPES_TOOLBAR ) ||
		!m_wndTBshapes.LoadToolBar(IDR_SHAPES))
	{
		TRACE0("Failed to create shapes toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndTBdemo.Create(	this, WS_CHILD | WS_VISIBLE | CBRS_TOP,
								ID_VIEW_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);

	// Shapes toolbar docking
	m_wndTBshapes.SetBarStyle(m_wndTBshapes.GetBarStyle() |
					CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
	m_wndTBshapes.EnableDocking(CBRS_ALIGN_ANY);
	EkDockBarNextTo(&m_wndTBshapes, &m_wndToolBar, AFX_IDW_DOCKBAR_TOP);
	m_wndTBshapes.SetWindowText( CString( LPCSTR( IDR_SHAPES ) ) );

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

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

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

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

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

BOOL CMainFrame::OnToggleBar(UINT nID)
{
	// 1 - Find the control bar that we should show or hide
	CControlBar* pBar = GetControlBar( nID );

	if( pBar != NULL )
	{
		// 2 - Toggle the control bar "visible" state
		ShowControlBar( pBar, !EkIsBarVisible( pBar ), FALSE );
		return TRUE;	// Command handled
	}

	return FALSE;		// Command not handled
}

void CMainFrame::OnUpdateBarMenu(CCmdUI* pCmdUI)
{
	// 1 - Find the control bar for which we are called
	CControlBar* pBar = GetControlBar( pCmdUI->m_nID );

	if( pBar != NULL )
	{
		// 2 - Set "checked" state depending on "visible" state
		pCmdUI->SetCheck( EkIsBarVisible( pBar ) );
		return;					// Command handled
	}

//	pCmdUI->ContinueRouting();	// Command not handled
}

void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	// 1 - Ensure right click was on a docking area
	if( pWnd->IsKindOf( RUNTIME_CLASS( CDockBar ) ) )
	{
		// 2 - Load top-level menu from resource
		CMenu mnuTop;
		mnuTop.LoadMenu( IDR_POPUP_MENU );

		// 3 - Get popup menu from first sub-menu
		CMenu* pPopup = mnuTop.GetSubMenu( 0 );
		ASSERT_VALID( pPopup );

		// Checked state for popup menu items is automatically
		// managed by standard MFC UPDATE_COMMAND_UI mechanism!

		// 4 - Display popup menu
		pPopup->TrackPopupMenu(	TPM_LEFTALIGN | TPM_LEFTBUTTON,
								point.x, point.y,
								this, NULL );

		// Popup menu commands are automatically handled
		// by standard MFC command-routing mechanism!

		return;
	}
}

⌨️ 快捷键说明

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