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

📄 movementview.cpp

📁 visual c++数字图像与图形处理中的光盘内容
💻 CPP
字号:
// MovementView.cpp : implementation of the CMovementView class
//

#include "stdafx.h"
#include "Movement.h"

#include "MovementDoc.h"
#include "MovementView.h"

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

#include "VirtualSphere.h"
#include "VirtualCylinder.h"
#include "Brick.h"

/////////////////////////////////////////////////////////////////////////////
// CMovementView

IMPLEMENT_DYNCREATE(CMovementView,  CView)

BEGIN_MESSAGE_MAP(CMovementView,  CView)
	//{{AFX_MSG_MAP(CMovementView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMovementView construction/destruction

CMovementView::CMovementView()
{
	// TODO: add construction code here
	x = 9;
	y = 30;
	m_nSpeedX = 9;
	m_nSpeedY = 1;

	m_bPlay = TRUE;
}

CMovementView::~CMovementView()
{
}

BOOL CMovementView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMovementView drawing

void CMovementView::OnDraw(CDC* pDC)
{
	CRect rect;
	GetClientRect(&rect);
	int nClientWidth = rect.Width();
	int nClientHeight = rect.Height();
	
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);

	CBitmap bitmap;
	bitmap.CreateCompatibleBitmap(pDC,  nClientWidth,  nClientHeight);
	CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
	CBrush* pBrush = new CBrush(RGB(0,  0,  0));
	memDC.FillRect(rect, pBrush);
	delete pBrush;


	//滑块宽度为50,  半长为25
	//柱子宽度为9;
	//球的半径为16;
	
	//x的初始值为 9,  最低值也为9
	
	DrawSphere(&memDC,  x,  y);
	
	//17 + 16 / 2 = 25
	DrawGlider(&memDC,  x - 17,  nClientHeight - 61);
	
	DrawBase(&memDC);
	DrawPillar(&memDC);

	//
	pDC->BitBlt(0,  0,  nClientWidth,  nClientHeight,  &memDC,  0,  0,  SRCCOPY);	
	memDC.SelectObject(pOldBitmap);
	bitmap.DeleteObject();

}

/////////////////////////////////////////////////////////////////////////////
// CMovementView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMovementView message handlers


//绘制基座
void CMovementView::DrawBase(CDC *pDC)
{
	CRect rect;
	GetClientRect(&rect);
	int nClientWidth = rect.Width();
	int nClientHeight = rect.Height();

	
	//砖块宽度----50, 
	//砖块高度----20

	//y坐标位置
	int y = nClientHeight - 20;
	
	//砖块数
	int nBricks = nClientWidth / 50;

	CBrick brick;

	//最后一排
	for(int i = 0; i < nBricks; i++)
	{
		brick.Whole(pDC,  (i * 50),  y,  50,  20);
	}
	
	if((nClientWidth - 50 * nBricks) > 0)
		brick.RightCutted(pDC,  i * 50,  y,  (nClientWidth - 25 * nBricks),  20);

	//最上一排
	y -= 20;
	brick.LeftCutted(pDC,  0,  y,  25,  20);
	
	nBricks = (nClientWidth - 25) / 50;
	for(i = 0; i < nBricks; i++)
	{
		brick.Whole(pDC,  25 + (i * 50),  y,  50,  20);
	}
	
	if((nClientWidth - 50 * nBricks - 25) > 0)
		brick.RightCutted(pDC,  25 + i * 50,  y,  (nClientWidth - 25 * nBricks),  20);

	//绘制一条线
	y--;
	CPen pen(PS_SOLID,  1,  RGB(255,  0,  0));
	CPen* pOldPen = pDC->SelectObject(&pen);
	
	//因为柱宽为9
	pDC->MoveTo(9,  y);
	pDC->LineTo(nClientWidth - 9,  y);

	pDC->SelectObject(pOldPen);
	pen.DeleteObject();

}

//绘制两边的柱子
void CMovementView::DrawPillar(CDC *pDC)
{
	CRect rect;
	GetClientRect(&rect);
	int nClientWidth = rect.Width();
	int nClientHeight = rect.Height();

	//柱子宽度-----15
	CVirtualCylinder vc;
	vc.SetColor(128,  32,  16);

	//左柱子
	vc.Draw(pDC,  0,  0,  9,  nClientHeight - 40);

	//右柱子
	vc.Draw(pDC,  nClientWidth - 9,  0,  9,  nClientHeight - 40);
}

//绘制滑块(运动部件)
void CMovementView::DrawGlider(CDC *pDC,  int x,  int y)
{
	CBrick brick;
	brick.SetColor(32,  128,  255);
	brick.Whole(pDC,  x,  y,  50,  20);
}

//绘制小球(运动部件)
void CMovementView::DrawSphere(CDC *pDC,  int x,  int y)
{
	//半径为 16 个像素单位
	//其它采用缺省设置
	CVirtualSphere vs(16);
	vs.SetColor(255,  255,  255);

	vs.Draw(pDC,  x,  y);
}

int CMovementView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	SetTimer(1,  45,  NULL);
	
	return 0;
}

void CMovementView::OnDestroy() 
{
	CView::OnDestroy();
	
	if(m_bPlay)	KillTimer(1);
	
}

void CMovementView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	CRect rect;
	GetClientRect(&rect);
	int nClientWidth = rect.Width();
	int nClientHeight = rect.Height();
	
	x += m_nSpeedX;
	
	//控制 x 坐标
	if( x > nClientWidth - 25)
	{
		x = nClientWidth - 25;
		m_nSpeedX = -m_nSpeedX;
	}
	else if(x < 9)
	{
		x = 9;
		m_nSpeedX = -m_nSpeedX;
	}

	//控制 y 坐标
	y += m_nSpeedY;
	m_nSpeedY++;

	if(y > (nClientHeight - 76))
	{
		y = 2 * (nClientHeight - 76) - y;
		m_nSpeedY = -m_nSpeedY + 4;
	}
	else if( y < 30)y = 30;

	Invalidate(FALSE);

	CView::OnTimer(nIDEvent);
}

void CMovementView::OnLButtonDown(UINT nFlags,  CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	m_bPlay = !m_bPlay;

	if(m_bPlay)
		SetTimer(1,  45,  NULL);
	else
		KillTimer(1);

	CView::OnLButtonDown(nFlags,  point);
}

void CMovementView::OnRButtonDown(UINT nFlags,  CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	x = 9;
	y = 30;
	m_nSpeedX = 9;
	m_nSpeedY = 1;
	
	CView::OnRButtonDown(nFlags,  point);
}

⌨️ 快捷键说明

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