代码地址:http://download.youkuaiyun.com/detail/liu_liu213/4013211
/************************************************************************/
/* 列编号 = 索引值 / 每一列的图块个数
行编号 = 索引值 % 每一列的图块个数
左上点X坐标 = 行编号* 图块宽度
左上点Y坐标 = 列编号* 图块高度
*/
/************************************************************************/
#include "stdafx.h"
//全局变量声明
HINSTANCE hInst;
HBITMAP fullmap;
HDC mdc;
const int rows=8, cols=8;
//全局函数
void MyPaint(HDC hdc);
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//****程序入口**************************************
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
//运行初始化函数
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
//消息循环
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
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 = "canvas"; //类别名称
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);
}
//****初始化*************************************
// 1.存储instance handle与全局变量中
// 2.建立并显示主窗口
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
HDC hdc, bufdc;
hInst = hInstance;
hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
MoveWindow(hWnd,10,10,600,450,true);
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, //
0,0,0,0,0,0,0,1, //
2,0,0,0,0,0,2,2, //
2,0,0,0,0,2,2,2, //
2,0,0,0,2,2,0,0, //
0,0,2,2,2,0,0,1, //
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);
SelectObject(mdc, fullmap);
HBITMAP map[3];
char filename[20] = "";
int rowNum,colNum;
int i, x, y;
//加载各图块位图
for (i=0; i<3; i++){
sprintf(filename, "map%d.bmp", i);
map[i] = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 50, 50, LR_LOADFROMFILE);
}
//按mapIndex数组中定义取出对应图块,进行地图拼接(列不是列数 行不是行数 而是相反的概率)
for(i=0; i<rows*cols; i++){
SelectObject(bufdc, map[mapIndex[i]]);
rowNum = i / cols; //列编号
colNum = i % cols; //行编号
x = colNum * 50;
y = rowNum * 50;
BitBlt(mdc, x, y, 50, 50, bufdc, 0, 0,SRCCOPY);
}
MyPaint(hdc);
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:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}