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

📄 compic.cpp

📁 别人做的一个拼图的小游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// compic.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include "compic.h"

#define MAX_LOADSTRING 100

HINSTANCE hInst;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];

ATOM			MyRegisterClass(HINSTANCE hInstance);
BOOL			InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_COMPIC, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_COMPIC);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}

//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
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			= LoadIcon(hInstance, (LPCTSTR)IDI_COMPIC);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_COMPIC;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   SYSTEMTIME sysTime;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, "拼图游戏", WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,
       100, 100, 804, 450+28, NULL, NULL, hInstance, NULL); 

   if (!hWnd)
   {
      return FALSE;
   }

   hWndMain = hWnd;
   gameStatus = GMINIT;

   if(OnInit(hWnd) == FALSE) return FALSE;

   ShowWindow(hWnd, nCmdShow);

   DragAcceptFiles(hWnd, true);
   UpdateWindow(hWnd);

   GetLocalTime(&sysTime);
   srand(sysTime.wHour*3600+sysTime.wMinute*60+sysTime.wSecond+sysTime.wMilliseconds);

   return TRUE;
}

//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent, i = 0, j = 0;
	PAINTSTRUCT ps;
	HDC hdc = NULL;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_NEW_GAME:           /* 新游戏 */
					gameStatus = GMGOING;
					OnNewGame(hdc);
					break;
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_MOUSEMOVE:
			mcur_y = LOWORD(lParam);
			mcur_x = HIWORD(lParam);
			break;
		case WM_SETCURSOR:
			OnSetCursor();
			break;
		case WM_LBUTTONDOWN:
			mouse_y = LOWORD(lParam);
			mouse_x = HIWORD(lParam);
			if(gameStatus != GMGOING) break;
			OnMouseClick(hdc);
			break;
		case WM_DROPFILES:
			OnDragFile(wParam);
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...
			OnPaint(hdc);
			EndPaint(hWnd, &ps);
			break;
		case WM_TIMER:
			showInfo();
			break;
		case WM_DESTROY:
			for(i = 0; i < LINE; i ++)
			{
				for(j = 0; j < ROW; j ++)
				{
					if(h_PicBm[i][j] != NULL)
						DeleteObject(h_PicBm[i][j]);
				}
			}
			if(h_SmPicBm != NULL)
				DeleteObject(h_SmPicBm);
			PostQuitMessage(0);
			break;
		case WM_NCMOUSEMOVE:
			mcur_y = 0;
			mcur_x = 0;
			SetCursor(LoadCursor(NULL, (LPCTSTR)IDC_ARROW));
			return DefWindowProc(hWnd, message, wParam, lParam);
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

/* 初始化游戏参数 */
int OnInit(HWND hWnd)
{
	int i = 0, j = 0;
	char picName[256], sFile[256];
	HDC hDC;

	/* 初始化背景坐标 */
	for(i = 0; i < LINE; i ++)
	{
		for(j = 0; j < ROW; j++)
		{
			h_PicBm[i][j] = NULL;
			map[i][j].x = i;
			map[i][j].y = j;
			map[i][j].status = 0;
			map[i][j].px = i;
			map[i][j].py = j;
		}
	}

	GetClientRect(hWndMain, &rc);
	hDC = GetDC(hWnd);
	h_MemDC = CreateCompatibleDC(hDC);
	h_CliBm = CreateCompatibleBitmap(hDC, rc.right-rc.left, rc.bottom-rc.top);
	ReleaseDC(hWnd, hDC);

	hPen = CreatePen(PS_SOLID, 3, RGB(0, 0, 245));
	hBrush = CreateSolidBrush(RGB(0, 150, 0));
	hBushText = CreateSolidBrush(RGB(0, 0, 255));

	x_cell = (rc.bottom-rc.top)/LINE;
	y_cell = (rc.right - 266)/ROW;

	gameStatus = GMINIT;
	intSteps = 0;
	startT = 0;

	memset(sFile, 0x00, sizeof(sFile));
	strcpy(sFile, "C:\\Program Files\\compic");
	mkdir(sFile);
	strcat(sFile, "\\pic");

	if(CutBmpFile("pic\\bkpic.bmp", sFile, LINE, ROW) < 0)
		return FALSE;

	/* 初始化图片内存位图 */
	for(i = 0; i < LINE; i ++)
	{
		for(j = 0; j < ROW; j ++)
		{
			memset(picName, 0x00, sizeof(picName));
			sprintf(picName, "%s_%d_%d.bmp", sFile, i, j);
			h_PicBm[i][j] = apiLoadImage(picName, x_cell, y_cell);
			if(NULL == h_PicBm[i][j])
			{
				MessageBox(hWndMain, "图片文件损坏,创建内存位图出错", "错误", MB_OK);
				return FALSE;
			}
			unlink(picName);
		}
	}

	/* 读取参考图 */
	h_SmPicBm = apiLoadImage("pic\\bkpic.bmp", 260, 210);
	if(NULL == h_SmPicBm)
	{
		MessageBox(hWndMain, "图片文件损坏,创建内存位图出错", "错误", MB_OK);
		return FALSE;
	}

	/* 读取背景图 */
	h_PicBkGnd = (HBITMAP)LoadImage(hInst, (LPCTSTR)IDB_BITMAP_BKGND, IMAGE_BITMAP, 
		ROW*y_cell+2, rc.bottom, LR_DEFAULTCOLOR);
	if(NULL == h_PicBkGnd)
	{
		MessageBox(hWndMain, "图片文件损坏,创建内存位图出错", "错误", MB_OK);
		return FALSE;
	}

	/* 填充背景色和绘制分割线 */
	SelectObject(h_MemDC, hPen);
	SelectObject(h_MemDC, h_CliBm);

	FillRect(h_MemDC, &rc, hBrush);

	MoveToEx(h_MemDC, rc.right-265, rc.top, NULL);
	LineTo(h_MemDC, rc.right-265, rc.bottom);

	MoveToEx(h_MemDC, rc.right-265, rc.top+215, NULL);
	LineTo(h_MemDC, rc.right, rc.top+215);

	MoveToEx(h_MemDC, rc.right-265, rc.top+245, NULL);
	LineTo(h_MemDC, rc.right, rc.top+245);

	MoveToEx(h_MemDC, rc.right-265, rc.top+275, NULL);
	LineTo(h_MemDC, rc.right, rc.top+275);

	/* 设置定时器 */
	SetTimer(hWndMain, IDT_TIMER, 100, (TIMERPROC)NULL);

	displayText(rc.right-165, rc.top+220, "参考图片", 60);
	showInfo();

	return TRUE;
}

/* 拖文件到窗口消息处理,如果拖动多个文件,此处只处理一个文件 */
void OnDragFile(WPARAM wParam)
{
	char sFileName[256], sFile[256], picName[256];
	int i = 0, j = 0;
	RECT rt;
	HBITMAP h_TmpBm[LINE+5][ROW+5], h_SmBm = NULL;  

	memset(sFileName, 0x00, sizeof(sFileName));
	memset(sFile, 0x00, sizeof(sFile));


	strcpy(sFile, "C:\\Program Files\\compic\\pic");

	/* 获取窗口接受的文件名 */
	DragQueryFile((HDROP)wParam,  0, sFileName, sizeof(sFileName)-1);

	/* 分割位图 */
	if(CutBmpFile(sFileName, sFile, LINE, ROW) < 0)
		return;

	/* 读取位图数据 */
	h_SmBm = apiLoadImage(sFileName, 260, 210);
	if(NULL == h_SmBm)
	{
		MessageBox(hWndMain, "图片文件损坏,创建内存位图出错", "错误", MB_OK);
		return;
	}

	for(i = 0; i < LINE; i ++)
	{
		for(j = 0; j < ROW; j ++)
		{
			memset(picName, 0x00, sizeof(picName));
			sprintf(picName, "%s_%d_%d.bmp", sFile, i, j);
			h_TmpBm[i][j] = NULL;
			h_TmpBm[i][j] = apiLoadImage(picName, x_cell, y_cell);
			if(NULL == h_TmpBm[i][j])
			{
				MessageBox(hWndMain, "图片文件损坏,创建内存位图出错", "错误", MB_OK);
				unlink(picName);
				return;
			}
			unlink(picName);
		}
	}

	for(i = 0; i < LINE; i ++)
	{
		for(j = 0; j < ROW; j ++)
		{
			map[i][j].x = i;
			map[i][j].y = j;
			map[i][j].status = 0;
			map[i][j].px = i;
			map[i][j].py = j;

			DeleteObject(h_PicBm[i][j]);
			h_PicBm[i][j] = h_TmpBm[i][j];
		}
	}

	DeleteObject(h_SmPicBm);
	h_SmPicBm = h_SmBm;

	rt.left = rc.right-230;
	rt.top = rc.top+285;
	rt.bottom = rc.top+345;
	rt.right = rc.right;

	/* 清除统计信息 */
	SelectObject(h_MemDC, h_CliBm);
	FillRect(h_MemDC, &rt, hBrush);

	InvalidateRect(hWndMain, &rc, FALSE);

	gameStatus = GMINIT;
}

HBITMAP apiLoadImage(char *pName, int cx, int cy)
{
	HBITMAP h_TmpBm;
	h_TmpBm = (HBITMAP)LoadImage(hInst, pName, IMAGE_BITMAP, 
		cx, cy, LR_LOADFROMFILE);
	return h_TmpBm;
}

void apiBitBlt(HDC hDC, int x, int y, int cx, int cy)
{
	BitBlt(hDC, x, y, cx, cy, h_MemDC, 0, 0, SRCCOPY);
}

/* 绘制单个图片 */
void drawPic(HDC hDC, int x, int y)
{
	int px =0, py = 0;

	if(y < 0 || y >= ROW || x < 0 || x >= LINE) return;

	px = map[x][y].px;
	py = map[x][y].py;
	SelectObject(h_MemDC, h_PicBm[px][py]);
	apiBitBlt(hDC, y*y_cell + 2, x*x_cell + 1, y_cell-1, x_cell-1);
}

⌨️ 快捷键说明

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