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

📄 canvasframe.cpp

📁 《Visual C++游戏设计入门》的配套代码
💻 CPP
字号:
// canvasFrame.cpp : implementation file
//

#include "stdafx.h"
#include "canvasr.h"
#include "canvasFrame.h"

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

/////////////////////////////////////////////////////////////////////////////
// canvasFrame
IMPLEMENT_DYNCREATE(canvasFrame, CFrameWnd)
float x=0,y=0,vx=21,vy=21,ax=0.03,ay=0.03;
canvasFrame::canvasFrame()
{
	Create(NULL,"绘图窗口");
	CClientDC dc(this);
	int width = dc.GetDeviceCaps(HORZRES);
	int height = dc.GetDeviceCaps(VERTRES);
	GetWindowRect( &rect );
	width = ( width - ( rect.right - rect.left ))/2 ;
	height = (height - (rect.bottom - rect.top ))/2 ;
	MoveWindow( width , height , (rect.right - rect.left ) , (rect.bottom - rect.top ) ,true);
	GetClientRect(&rect);
	mdc = new CDC;
	mdc->CreateCompatibleDC(&dc);
	ball = new CBitmap;
	ball->m_hObject = (HBITMAP)::LoadImage(NULL,"ball.bmp",IMAGE_BITMAP,81,81,LR_LOADFROMFILE); //载入小球图
	mdc->SelectObject(ball); //选择位图到 mdc 中
}

canvasFrame::~canvasFrame()
{
	delete mdc;
	delete ball;
}


BEGIN_MESSAGE_MAP(canvasFrame, CFrameWnd)
	//{{AFX_MSG_MAP(canvasFrame)
	ON_WM_TIMER()
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// canvasFrame message handlers

void canvasFrame::OnTimer(UINT nIDEvent) 
{
	CFrameWnd::OnTimer(nIDEvent);
	CClientDC dc(this);
	dc.BitBlt(x,y,81,81,mdc,0,0,SRCCOPY);  //贴上小球图
	if(vx>0)       //vx 为正值
	{
		vx-=ax;    
		if(vx<0)
			vx=0;
	}
	else           //vx 为负值
	{
		vx+=ax;
		if(vx>0)
			vx=0;
	}
	if(vy>0)       //vy 为正值
	{
		vy-=ay;
		if(vy<0)
			vy=0;
	}
	else           //vy 为负值
	{
		vy+=ay;
		if(vy>0)
			vy=0;
	}
	x += vx;       //计算新的 X 坐标
	y += vy;       //计算新的 Y 坐标
	if(vx==0 && vy==0)
	{	vx=21;
	    vy=21;
		//KillTimer(nIDEvent);
	}
	if(y+60 > rect.bottom)  //是否碰到下边缘
	{
		vy = -vy;
		y = rect.bottom -60;
	}
	if(x+60 > rect.right)   //是否碰到右边缘
	{
		vx = -vx;
		x = rect.right -60;
	}
	if(y < -21)             //是否碰到上边缘
	{
		vy = -vy;
		y = -21;
	}
	if(x < -21)             //是否碰到左边缘
	{
		vx = -vx;
		x =- 21;
	}
}

int canvasFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	SetTimer(1,0,NULL);
	return 0;
}

⌨️ 快捷键说明

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