ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
int mapIndex[rows*cols] = { 2,2,2,2,0,1,0,1, //第1列
0,2,2,0,0,0,1,1, //第2列
0,0,0,0,0,0,0,1, //第3列
2,0,0,0,0,0,2,2, //第4列
2,0,0,0,0,2,2,2, //第5列
2,0,0,0,2,2,0,0, //第6列
0,0,2,2,2,0,0,1, //第7列
0,0,2,0,0,0,1,1 };//第8列
hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
fullmap = CreateCompatibleBitmap(hdc,cols*50,rows*50); //先建立fullmap为空白的位图,将其宽与高分别为“行数*图块宽”与“列数*图块高”。
SelectObject(mdc,fullmap); //将fullmap存入mdc中
HBITMAP map[3];
char filename[20] = "";
int rowNum,colNum;
int i,x,y;
//加载各块位图
for(i=0;i<3;i++) //利用循环转换图文文件名,取出各个图块存与“map[i]”中。图块文件名为“map0.bmp”和“map1.bmp”等。
{
sprintf(filename,"map%d.bmp",i);
map[i] = (HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
}
//按照mapIndex数组中的定义取出对应图块,进行地图拼接
for (i=0;i
SelectObject(bufdc,map[mapIndex[i]]); //根据mapIndex[i]中的代号选取对应的图块到bufdc中。代号为“0”则取“map[0]”,以此类推
rowNum = i / cols; //求列编号
colNum = i % cols; // 求行编号
x = colNum * 50; // 求贴图X坐标
y = rowNum * 50; // 求贴图Y坐标
BitBlt(mdc,x,y,50,50,bufdc,0,0,SRCCOPY); //在mdc上进行贴图
}
MyPaint(hdc); //当上面代码的循环完成在mdc上的图块贴图之后,fullmap便是拼接出来的地图,此时再调用MyPaint()函数进行窗口贴图。
ReleaseDC(hWnd,hdc);
DeleteDC(bufdc);
return TRUE;
}
//****自定义绘图函数*********************************
void MyPaint(HDC hdc)
{
//贴上拼接后的组合地图
SelectObject(mdc,fullmap);
BitBlt(hdc,10,10,cols*50,rows*50,mdc,0,0,SRCCOPY);//在窗口中贴上拼接后的组合地图,整个地图的贴图大小按照拼接地图的行数、列数和图块的宽高来决定。
}
//****消息处理函数***********************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT: //窗口重绘消息
hdc = BeginPaint(hWnd, &ps);
MyPaint(hdc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY: //窗口结束消息
DeleteDC(mdc);
DeleteObject(fullmap);
PostQuitMessage(0);
break;
default: //其他消息
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
#include "stdafx.h"
#include
//全局变量声明
HINSTANCE hInst;
HBITMAP fullmap; //声明fullmap位图对象,在初始函数中完成的地图会存到这个位图中
HDC mdc;
//行列数声明
const int rows = 8,cols = 8;
//全局函数的声明
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
//***WinMain函数,程序入口点函数**************************************
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
//运行初始化函数
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
//消息循环 www.2