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

📄 borderbar.cpp

📁 这是MFC经典问答书的光盘内容
💻 CPP
字号:
// BorderBar.cpp : implementation file
//

#include "stdafx.h"
#include "CustomBars.h"
#include "BorderBar.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBorderBar

CBorderBar::CBorderBar()
{
	m_nPosition = 0;
}

CBorderBar::~CBorderBar()
{
}


BEGIN_MESSAGE_MAP(CBorderBar, CControlBar)
	//{{AFX_MSG_MAP(CBorderBar)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CBorderBar message handlers

CSize CBorderBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
{
	CSize size = CControlBar::CalcFixedLayout( bStretch, bHorz );

	if( bHorz )
	{
		// Bar is horizontal, set height
		size.cy = HEIGHT;	// defined in CBorderBar class
	}
	else
	{
		// Bar is vertical, set width
		size.cx = WIDTH;	// defined in CBorderBar class
	}

	return size;
}

BOOL CBorderBar::Create(	CWnd* pParentWnd, DWORD dwStyle,
							UINT nIDBar, UINT nIDBitmap )
{
	ASSERT_VALID(pParentWnd);   // must have a parent

	// 1 - Style must contain CBRS_TOP or CBRS_BOTTOM
	// or CBRS_LEFT or CBRS_RIGHT
	ASSERT( (dwStyle & CBRS_TOP) || (dwStyle & CBRS_BOTTOM)
			|| (dwStyle & CBRS_LEFT) || (dwStyle & CBRS_RIGHT) );

	// 2 - Save bar position from style CBRS_xxx bits
	if( dwStyle & CBRS_TOP )
	{
		ASSERT( m_nPosition == 0 );
		m_nPosition = CBRS_TOP;
	}

	if( dwStyle & CBRS_BOTTOM )
	{
		ASSERT( m_nPosition == 0 );
		m_nPosition = CBRS_BOTTOM;
	}
	
	if( dwStyle & CBRS_LEFT )
	{
		ASSERT( m_nPosition == 0 );
		m_nPosition = CBRS_LEFT;
	}

	if( dwStyle & CBRS_RIGHT )
	{
		ASSERT( m_nPosition == 0 );
		m_nPosition = CBRS_RIGHT;
	}

	ASSERT( m_nPosition != 0 );

	// 3 - Save the style
	dwStyle |= WS_CHILD | WS_VISIBLE | CBRS_SIZE_FIXED;
	m_dwStyle = dwStyle;

	dwStyle &= ~CBRS_ALL;
	dwStyle |= CCS_NOPARENTALIGN|CCS_NOMOVEY|CCS_NODIVIDER|CCS_NORESIZE;

	// 4 - Load bitmap
	if( !m_Bitmap.LoadBitmap( nIDBitmap ) )
	{
		TRACE1( "Unable to load border bar bitmap with ID = %d\n", nIDBitmap );
		return FALSE;
	}

	// 5 - Finally create the window
	CRect rc;
	rc.SetRectEmpty();
	if( !CWnd::Create( NULL, NULL, dwStyle, rc, pParentWnd, nIDBar ) )
	{
		TRACE0( "Unable to create border bar window\n" );
		return FALSE;
	}

	return TRUE;
}

void CBorderBar::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	// 1 - Compute paint area
	CRect rc;
	GetClientRect( &rc );

	// 2 - Create compatible memory DC
	CDC dcMem;
	dcMem.CreateCompatibleDC( &dc );

	// 3 - Select bitmap into memory DC
	CBitmap* pOldBitmap = dcMem.SelectObject( &m_Bitmap );

	// 4 - Compute bitmap size
	BITMAP bitmap;
	m_Bitmap.GetObject( sizeof( BITMAP ), &bitmap);
	CSize sizeBitmap( bitmap.bmWidth, bitmap.bmHeight );

	// 5 - Blit the bitmap from the memory DC to the real DC
	dc.StretchBlt( rc.left, rc.top, rc.Width(), rc.Height(),
			 &dcMem, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY );

	// 6 - Deselect bitmap from memory DC
	dcMem.SelectObject( pOldBitmap );
}

void CBorderBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
{
	// Update the dialog controls added to the control bar (if any)
	UpdateDialogControls(pTarget, bDisableIfNoHndler);
}

⌨️ 快捷键说明

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