卷轴式游戏地图实现

运行效果:

卷轴式游戏地图实现

完整工程下载地址:http://download.csdn.net/detail/qq2399431200/6267291 0积分

以下是完整程序代码:

 

#include "stdafx.h"



//全局变量

HINSTANCE hInst;

HBITMAP map;

HDC		hdc,mdc;

HWND	hWnd;

DWORD	tPre,tNow;

int		x=730,y=340;



//函数声明

ATOM				MyRegisterClass(HINSTANCE hInstance);

BOOL				InitInstance(HINSTANCE, int);

LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);

void				MyPaint(HDC hdc);



//主函数

int APIENTRY WinMain(HINSTANCE hInstance,

                     HINSTANCE hPrevInstance,

                     LPSTR     lpCmdLine,

                     int       nCmdShow)

{

	MSG msg;



	MyRegisterClass(hInstance);



	//初始化

	if (!InitInstance (hInstance, nCmdShow)) 

	{

		return FALSE;

	}



	//游戏循环

    do//while( msg.message!=WM_QUIT )

    {

        if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )

        {

            TranslateMessage( &msg );

            DispatchMessage( &msg );

        }

		else

		{

			tNow = GetTickCount();

			if(tNow-tPre >= 40)

				MyPaint(hdc);

		}

    }while( msg.message!=WM_QUIT );



	return msg.wParam;

}



//定义及注册视窗类别函数

ATOM MyRegisterClass(HINSTANCE hInstance)

{

	WNDCLASSEX wcex;



	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;

	wcex.lpfnWndProc	= (WNDPROC)WndProc;

	wcex.cbClsExtra		= 0;

	wcex.cbWndExtra		= 0;

	wcex.hInstance		= hInstance;

	wcex.hIcon			= NULL;

	wcex.hCursor		= NULL;

	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);

	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);

	wcex.lpszMenuName	= NULL;

	wcex.lpszClassName	= "seek";   //窗口类名称

	wcex.hIconSm		= NULL;



	return RegisterClassEx(&wcex);  //注册窗口类别

}



//创建窗体以及加载背景

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

{

	hInst = hInstance;



	hWnd = CreateWindow("seek", "卷轴-世界地图查看器V1.1" , WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX,  //禁用最大化按钮

		CW_USEDEFAULT, 0, CW_USEDEFAULT,0, NULL, NULL, hInstance, NULL);



	if (!hWnd)

	{

		return FALSE;

	}



	MoveWindow(hWnd,100,80,640,480,true);

	ShowWindow(hWnd, nCmdShow);

	UpdateWindow(hWnd);



	hdc = GetDC(hWnd);

	mdc = CreateCompatibleDC(hdc);



	map = (HBITMAP)LoadImage(NULL,"map.bmp",IMAGE_BITMAP,2792,1831,LR_LOADFROMFILE);



	SelectObject(mdc,map);



	MyPaint(hdc);



	return TRUE;

}



//自定义绘图函数

void MyPaint(HDC hdc)

{

	BitBlt(hdc,0,0,640,480,mdc,x,y,SRCCOPY);

	tPre = GetTickCount();

}





//消息处理过程函数

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

	switch (message)

	{

	//鼠标左键单击按下事件

	case WM_LBUTTONDOWN:   

		x += ( LOWORD(lParam) - /*320*/240 );

		y += ( HIWORD(lParam) - 240 );

		

		//坐标矫正

		if( (y < 27 && (y = 27)) || (y > 1350 && (y = 1350)) )

				;//none

		if( (x < 27 && (x = 27)) || (x > 2158 && (x =2158)) )

				;//none

			break;

    //鼠标滑轮事件

	case WM_MOUSEWHEEL: 

         if( ((short)HIWORD(wParam) )> 0 ) //滑轮向上滚  (这个short强制转换非常重要,不然出问题)

         {

			 y -= 20;

			 if( y < 27 )

				 y = 27;

		 }

		 else //滑轮向下滚

		 {

			 y += 20;

			 if(y>1350)

				y=1350;

		 }

		break;

	//键盘消息处理

	case WM_KEYDOWN:	

		switch (wParam) 

		{

		case VK_UP:				 //龄

			y -= 20;

			if( y < 27 )

				y = 27;

			break;

		case VK_DOWN:			 //龄

			y += 20;

			if(y>1350)

				y=1350;

			break;

		case VK_LEFT:			 //オ龄

			x -= 20;

			if( x < 27 )

				x = 27;

			break;

		case VK_RIGHT:			 //龄

			x += 20;

			if( x > 2158)

				x = 2158;

			break;

		}

		break;

		//销毁窗体及退出程序

		case WM_DESTROY:					

			DeleteDC(mdc);

			DeleteObject(map);

			ReleaseDC(hWnd,hdc);

			PostQuitMessage(0);  //退出程序

			break;

		//其它消息默认处理

		default:

			return DefWindowProc(hWnd, message, wParam, lParam);

	}

   return 0;

}



 

警告:
如果用VC6(用VS2010没有次编译错误)编译可能出现:WM_MOUCEWHEEL没有定义的错误提示。

解决方法:添加如下宏定义

#define WM_MOUSEWHEEL 0x020A(编译通过)


有的网友说添加:#define _WIN32_WINNT 0x0400 可以解决,但是我尝试后没有解决这个问题,但是上面方案可以解决。

 



 

你可能感兴趣的:(游戏)