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

📄 blitdemoview.cpp

📁 Visual C++编程宝典随书光盘里的代码
💻 CPP
字号:
// BlitDemoView.cpp:implementation of CBlitDemoView class
//

#include "stdafx.h"
#include "BlitDemo.h"

#include "BlitDemoDoc.h"
#include "BlitDemoView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBlitDemoView

IMPLEMENT_DYNCREATE(CBlitDemoView, CView)

BEGIN_MESSAGE_MAP(CBlitDemoView, CView)
	//{{AFX_MSG_MAP(CBlitDemoView)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_OPERATIONS_MASKFLY,
		OnOperationsMaskfly)
	ON_COMMAND(ID_OPERATIONS_SHOWBUBBLES,
		OnOperationsShowbubbles)
	ON_COMMAND(ID_OPERATIONS_SHOWTROLLEY,
		OnOperationsShowtrolley)
	ON_COMMAND(ID_OPERATIONS_XORFLY,
		OnOperationsXorfly)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBlitDemoView construction/destruction

CBlitDemoView::CBlitDemoView()
{

	// Start the program showing bubbles.
	m_nOperation = OPERATION_BUBBLES;

	// Initialize our indexes to the fly,
	// bubble, and trolley.
	m_nCurrentFly = 2;
	m_nCurrentBubble = 0;
	m_nCurrentTrolley = 0;

	// Set the fly's position.
	m_nFlyX = 20;
	m_nFlyY = 10;

	static int nBubble[] = {
	IDB_BUBBLE1, IDB_BUBBLE2, IDB_BUBBLE3 };

	static int nFly[] = {
	IDB_FLY1, IDB_FLY2, IDB_FLY3, IDB_FLY4 };

	static int nFlyMask[] = {
	IDB_FLYMASK1, IDB_FLYMASK2,
	IDB_FLYMASK3, IDB_FLYMASK4 };

	static int nTrolley[] = {
	IDB_TROLLEY1, IDB_TROLLEY2,
	IDB_TROLLEY3, IDB_TROLLEY4 };

	// Load in the bitmaps using LoadBitmap().
	for( int i=0; i<4; i++ ){
		if( i < 3 )
			m_Bubble[i].LoadBitmap( nBubble[i] );
		m_Fly[i].LoadBitmap( nFly[i] );
		m_FlyMask[i].LoadBitmap( nFlyMask[i] );
		m_Trolley[i].LoadBitmap( nTrolley[i] );
		}

}

CBlitDemoView::~CBlitDemoView()
{
}

BOOL CBlitDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CBlitDemoView drawing

void CBlitDemoView::OnDraw(CDC* pDC)
{
	CBlitDemoDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// When a redraw message comes, just
	// call DrawBitmaps()
	DrawBitmaps( pDC );

}

void CBlitDemoView::DrawBitmaps( CDC *pDC )
{

	// Create a DC into which the bitmap
	// will be selected.
	CDC MemDC;
	MemDC.CreateCompatibleDC( pDC );
	CBitmap *pOldBitmap;

	BITMAP bm;

	switch( m_nOperation ){
		case OPERATION_BUBBLES:
			// Select the appropriate bitmap into
			// the DC.
			pOldBitmap =
				(CBitmap *) MemDC.SelectObject(
					&m_Bubble[m_nCurrentBubble] );
			// We need the width and height of the
			// bitmap for the BitBlt() function.
			m_Bubble[m_nCurrentBubble].GetObject(
				sizeof( BITMAP ), &bm );
			// Perform the BitBlt() function to the destination
			// coordinates 0, 0.
			pDC->BitBlt( 0, 0, bm.bmWidth, bm.bmHeight, &MemDC,
				0, 0, SRCCOPY );
			break;
		case OPERATION_TROLLEY:
			// Select the appropriate bitmap into
			// the DC.
			pOldBitmap =
				(CBitmap *) MemDC.SelectObject(
					&m_Trolley[m_nCurrentTrolley] );
			// We need the width and height of the
			// bitmap for the BitBlt() function.
			m_Trolley[m_nCurrentTrolley].GetObject(
				sizeof( BITMAP ), &bm );
			// Perform the BitBlt() function to the destination
			// coordinates 0, 0.
			pDC->BitBlt( 0, 0, bm.bmWidth, bm.bmHeight, &MemDC,
				0, 0, SRCCOPY );
			break;
		case OPERATION_XORFLY:
			// Select the appropriate bitmap into
			// the DC.
			pOldBitmap =
				(CBitmap *) MemDC.SelectObject(
					&m_Fly[m_nCurrentFly] );
			// We need the width and height of the
			// bitmap for the BitBlt() function.
			m_Fly[m_nCurrentFly].GetObject(
				sizeof( BITMAP ), &bm );
			// Perform the BitBlt() function to the destination
			// coordinates calculated by using m_nFlyX and m_nFlyY
			// as the center of the object. The width and height
			// of the bitmap are then used to draw the object
			// centered on m_nFlyX and m_nFlyY.
			pDC->BitBlt( m_nFlyX - ( bm.bmWidth / 2 ), m_nFlyY,
				bm.bmWidth, bm.bmHeight, &MemDC,
				0, 0, SRCINVERT );
			break;
		case OPERATION_MASKFLY:
			// Select the appropriate bitmap into
			// the DC.
			pOldBitmap =
				(CBitmap *) MemDC.SelectObject(
					&m_FlyMask[m_nCurrentFly] );
			// We need the width and height of the
			// bitmap for the BitBlt() function.
			m_FlyMask[m_nCurrentFly].GetObject(
				sizeof( BITMAP ), &bm );
			pDC->BitBlt( m_nFlyX - ( bm.bmWidth / 2 ), m_nFlyY,
				bm.bmWidth, bm.bmHeight, &MemDC,
				0, 0, SRCAND );
			MemDC.SelectObject( &m_Fly[m_nCurrentFly] );
			// Perform the BitBlt() function to the destination
			// coordinates calculated by using m_nFlyX and m_nFlyY
			// as the center of the object. The width and height
			// of the bitmap are then used to draw the object
			// centered on m_nFlyX and m_nFlyY.
			pDC->BitBlt( m_nFlyX - ( bm.bmWidth / 2 ), m_nFlyY,
				bm.bmWidth, bm.bmHeight, &MemDC,
				0, 0, SRCPAINT );
			break;
		}

	// Select the old bitmap back
	// into the DC.
	MemDC.SelectObject( pOldBitmap );

}

/////////////////////////////////////////////////////////////////////////////
// CBlitDemoView diagnostics

#ifdef _DEBUG
void CBlitDemoView::AssertValid() const
{
	CView::AssertValid();
}

void CBlitDemoView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CBlitDemoDoc* CBlitDemoView::GetDocument()
	// non-debug version is inline
{
	ASSERT(
		m_pDocument->IsKindOf(RUNTIME_CLASS(CBlitDemoDoc)));
	return (CBlitDemoDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CBlitDemoView message handlers

void CBlitDemoView::OnLButtonDown(UINT nFlags,
	CPoint point) 
{

	CClientDC ClientDC( this );

	switch( m_nOperation ){
		case OPERATION_BUBBLES:
			// Increment to next bubble.
			m_nCurrentBubble++;
			// Make sure the bubble value in in range.
			if( m_nCurrentBubble >= 3 )
				m_nCurrentBubble = 0;
			break;
		case OPERATION_TROLLEY:
			// Increment to next trolley.
			m_nCurrentTrolley++;
			// Make sure the trolley value is in range.
			if( m_nCurrentTrolley >= 4 )
				m_nCurrentTrolley = 0;
			break;
		case OPERATION_XORFLY:
			// XORFLY uses the same movement calculations
			// as MASKFLY. The only difference is that
			// to erase the previous image, XORFLY
			// calls DrawBitmaps() and MASKFLY draws
			// a rectangle.
			DrawBitmaps( &ClientDC );
		case OPERATION_MASKFLY:
			if( m_nOperation == OPERATION_MASKFLY ){
				// Get the bitmap size so that we
				// can calculate the rectangle which
				// we must draw to erase the fly.
				BITMAP bm;
				m_FlyMask[m_nCurrentFly].GetObject(
					sizeof( BITMAP ), &bm );
				RECT Rect;
				Rect.left = m_nFlyX - ( bm.bmWidth / 2 );
				Rect.top = m_nFlyY;
				Rect.right = Rect.left + bm.bmWidth;
				Rect.bottom = Rect.top + bm.bmHeight;
				// Create a brush that's the same as the
				// window color and then draw a rectangle.
				CBrush Brush( GetSysColor( COLOR_WINDOW ) );
				ClientDC.FillRect( &Rect, &Brush );
				}
			// We're toggling between 2 and 3 going down
			// and 0 and 1 going up. We can use ^ 1 to
			// accomplish this. If the fly moves past a
			// threshold point, we reverse the direction.
			m_nCurrentFly ^= 1;
			if( m_nCurrentFly >= 2 ){
				m_nFlyY += 10;
				if( m_nFlyY > 300 )
					m_nCurrentFly -= 2;
				}
			else{
				m_nFlyY -= 10;
				if( m_nFlyY <= 10 )
					m_nCurrentFly += 2;
				}
			break;
		}
	
	DrawBitmaps( &ClientDC );

	CView::OnLButtonDown(nFlags, point);
}

void CBlitDemoView::OnOperationsMaskfly() 
{

	// Set the operation to OPERATION_MASKFLY.
	m_nOperation = OPERATION_MASKFLY;
	// Invalidate the client window and force
	// an immediate redraw.
	InvalidateRect( NULL, TRUE );
	UpdateWindow();

}

void CBlitDemoView::OnOperationsShowbubbles() 
{

	// Set the operation to OPERATION_BUBBLES.
	m_nOperation = OPERATION_BUBBLES;
	// Invalidate the client window and force
	// an immediate redraw.
	InvalidateRect( NULL, TRUE );
	UpdateWindow();

}

void CBlitDemoView::OnOperationsShowtrolley() 
{

	// Set the operation to OPERATION_TROLLEY.
	m_nOperation = OPERATION_TROLLEY;
	// Invalidate the client window and force
	// an immediate redraw.
	InvalidateRect( NULL, TRUE );
	UpdateWindow();

}

void CBlitDemoView::OnOperationsXorfly() 
{

	// Set the operation to OPERATION_XORFLY.
	m_nOperation = OPERATION_XORFLY;
	// Invalidate the client window and force
	// an immediate redraw.
	InvalidateRect( NULL, TRUE );
	UpdateWindow();

}

⌨️ 快捷键说明

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