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

📄 canvasframe.cpp

📁 《Visual C++游戏设计入门》的配套代码
💻 CPP
字号:
// canvasFrame.cpp : implementation file
//
#define DIRECTINPUT_VERSION 0x0700
#include "stdafx.h"
#include "canvasr.h"
#include "canvasFrame.h"
#include "dinput.h"

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

struct bullet          
{
   int   x;           
   int   y;
   BOOL exist;
};

/////////////////////////////////////////////////////////////////////////////
// canvasFrame

IMPLEMENT_DYNCREATE(canvasFrame, CFrameWnd)

LPDIRECTINPUT7  pDI;        //声明 DirectInput 对象指针
LPDIRECTINPUTDEVICE7  pDMO; //声明输入装置对象指针
HRESULT result;             //声明 HRESULT 类型变量
DIMOUSESTATE2 MState;       //鼠标状态结构  
int x,y,xlast,ylast;
bullet b[200];
int bcount=0;
int i;

canvasFrame::canvasFrame()
{
	int cx,cy;
	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);
	ClientToScreen(&rect);    //转换坐标
	::ClipCursor(&rect);      //设定鼠标移动范围
	mdc = new CDC;
	mdc1 = new CDC;
	mdc->CreateCompatibleDC(&dc);
	mdc1->CreateCompatibleDC(&dc);
	bitmap = new CBitmap;
	bitmap->m_hObject = (HBITMAP)::LoadImage(NULL,"ship.bmp",IMAGE_BITMAP,100,74,LR_LOADFROMFILE); //载入图文件
	mdc->SelectObject(bitmap);
	bitmap->m_hObject = (HBITMAP)::LoadImage(NULL,"bullet1.bmp",IMAGE_BITMAP,32,15,LR_LOADFROMFILE); //载入图文件
	mdc1->SelectObject(bitmap);
	x = ((rect.right-rect.left) - 100)/2;    
	y = ((rect.bottom-rect.top) - 100)/2;   
	cx = x + 81/2;
	cy = y + 81/2;
	CPoint *p = new CPoint(cx,cy);
	ClientToScreen(p);               //转换坐标
	::ShowCursor(false);             //取消鼠标光标
	::SetCursorPos(p->x,p->y);       //设定鼠标位置
	ScreenToClient(&rect);           //转换坐标
	delete p;
}

canvasFrame::~canvasFrame()
{
	delete bitmap;
	delete mdc;
	delete mdc1;
	pDMO->Release();
	pDI->Release();
	::ClipCursor(NULL);
}

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

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

int canvasFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	HINSTANCE hinst = AfxGetInstanceHandle();  //取得应用程序的 handle
	result = DirectInputCreateEx(hinst, DIRECTINPUT_VERSION, 
        IID_IDirectInput7, (void**)&pDI, NULL); //建立 DirectInput 对象
	if(result != DI_OK)
		MessageBox("建立 DirectInput 对象失败!");
	result = pDI->CreateDeviceEx(GUID_SysMouse, IID_IDirectInputDevice7,
        (void**)&pDMO, NULL); //建立输入装置对象
	if(result != DI_OK)
		MessageBox("建立鼠标输入装置失败!");
	result = pDMO->SetDataFormat(&c_dfDIMouse2);
	if(result != DI_OK)
		MessageBox("设定数据格式失败!");
	result = pDMO->SetCooperativeLevel(m_hWnd, 
                   DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);  //设定协调层级
	if(result != DI_OK)
		MessageBox("设定程序协调层级失败!");
	result = pDMO->Acquire();   //取用输入装置
	if(result != DI_OK)
		MessageBox("取用输入装置失败!");
	SetTimer(500,1,NULL);
	// TODO: Add your specialized creation code here
	return 0;
}

void canvasFrame::OnTimer(UINT nIDEvent)
{
	CFrameWnd::OnTimer(nIDEvent);
	CClientDC dc(this);    
	result = pDMO->GetDeviceState(sizeof(MState),(LPVOID)&MState); //取得鼠标状态
	if(result != DI_OK )
		MessageBox("取得鼠标状态失败!");
	x += MState.lX;      //设定飞机图示的 x 坐标
	y += MState.lY;      //设定飞机图示的 y 坐标
	if(x<rect.left)      //是否已至左边界
		x = rect.left;
	if(x>rect.right-100) //是否已至右边界
		x = rect.right-100;
	if(y<rect.top)       //是否已至上边界
		y = rect.top;
	if(y>rect.bottom-74) //是否已至下边界
		y = rect.bottom-74;
	dc.BitBlt(xlast,ylast,100,74,mdc,0,0,WHITENESS); //覆盖上次的贴图
	xlast = x;
	ylast = y;
	if(MState.rgbButtons[0] & 0x80)  //判断是否按下鼠标左键
	{
		for(i=0;i<200;i++)
		{
			if(b[i].exist == false)  //加入一颗新子弹
			{
				b[i].x = x;
				b[i].y = y+30;
				b[i].exist = true;
				bcount++;
				break;
			}
		}
	}
	if(bcount != 0)
		for(i=0;i<200;i++)          //贴上所有子弹
		{
			dc.BitBlt(b[i].x,b[i].y,32,15,mdc1,0,0,SRCCOPY);
			b[i].x -=17;
			if(b[i].x<-30)
			{
				b[i].exist = false;
				bcount--;
			}
		}
	dc.BitBlt(x,y,100,74,mdc,0,0,SRCCOPY); //贴上飞机
}

void canvasFrame::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default

	CFrameWnd::OnChar(nChar, nRepCnt, nFlags);
	if( nChar== VK_ESCAPE )      //判断是否按下 Esc 键
		PostMessage(WM_CLOSE );  //传送WM_CLOSE信息
}


⌨️ 快捷键说明

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