// 扫雷.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "扫雷.h"
#include "MineMap.h"
#include<windowsx.h>
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
CMineMap m_Map;
int nflag = 0;
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK Setting(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_MY);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
int l = m_Map.mx * 20 + 16;
int r = m_Map.my * 20 + 99;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, l, r, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc, hdcMem;
static HBITMAP hBitmap,bmpfame,bmpface,bmpnub;
int px, py, n, m;
px = GET_X_LPARAM(lParam);
py = GET_Y_LPARAM(lParam);
m = px / 20;
n = py / 20 - 2;
CRect rc(20 * m, 20 * n + 40, 20 * m + 20, 20 * n + 60),crc;
static clock_t clnow=0;
static int x = m;
static int y = n;
int rm = m_Map.rMine,rt=m_Map.timer ;
switch (message)
{
case WM_CREATE:
m_Map.Create();
hBitmap = (HBITMAP)LoadImage((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), TEXT("项目1.bmp"),
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
bmpnub = (HBITMAP)LoadImage((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), TEXT("项目2.bmp"),
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
bmpfame = (HBITMAP)LoadImage((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), TEXT("项目3.bmp"),
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
bmpface = (HBITMAP)LoadImage((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), TEXT("项目4.bmp"),
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
SetTimer(hWnd, 1, 1000, NULL);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case ID_BEGAIN:
m_Map.Create();
InvalidateRect(hWnd, CRect(0, 0, m_Map.mx * 20, m_Map.my * 20 + 40), 0);
break;
case ID_32776:
if (DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, Setting))
{
GetWindowRect(hWnd, &crc);
MoveWindow(hWnd, crc.left, crc.top, m_Map.mx * 20 + 16, m_Map.my * 20 + 99, 1);
InvalidateRect(hWnd, CRect(0, 0, m_Map.mx * 20, m_Map.my * 20 + 40), 0);
}
break;
case ID_32773:
m_Map.mx = 10;
m_Map.my = 8;
m_Map.nMime = 15;
m_Map.Create();
GetWindowRect(hWnd,&crc);
MoveWindow(hWnd, crc.left, crc.top, m_Map.mx * 20 + 16, m_Map.my * 20 + 99, 1);
break;
case ID_32774:
m_Map.mx = 20;
m_Map.my = 18;
m_Map.nMime = 50;
m_Map.Create();
GetWindowRect(hWnd, &crc);
MoveWindow(hWnd, crc.left, crc.top, m_Map.mx * 20 + 16, m_Map.my * 20 + 99, 1);
break;
case ID_32775:
m_Map.mx = 30;
m_Map.my = 20;
m_Map.nMime = 100;
m_Map.Create();
GetWindowRect(hWnd, &crc);
MoveWindow(hWnd, crc.left, crc.top, m_Map.mx * 20 + 16, m_Map.my * 20 + 99, 1);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBitmap);
int i, j;
for (i = 0; i < m_Map.mx; i++)
{
for (j = 0; j < m_Map.my; j++)
{
if (m_Map.Map[i][j] >= 0 && m_Map.Map[i][j] < 9)
{
BitBlt(hdc, i * 20, j * 20 + 40, 20, 20, hdcMem, m_Map.Map[i][j] * 20, 0, SRCCOPY);
}
if (m_Map.Map[i][j]>90)
BitBlt(hdc, i * 20, j * 20 + 40, 20, 20, hdcMem, 10 * 20, 0, SRCCOPY);
if (m_Map.Map[i][j] == -1)
BitBlt(hdc, i * 20, j * 20 + 40, 20, 20, hdcMem, 9 * 20, 0, SRCCOPY);
if (m_Map.Map[i][j] == -2)
BitBlt(hdc, i * 20, j * 20 + 40, 20, 20, hdcMem, 12 * 20, 0, SRCCOPY);
if (m_Map.Map[i][j] > 40 && m_Map.Map[i][j] < 60)
BitBlt(hdc, i * 20, j * 20 + 40, 20, 20, hdcMem, 11 * 20, 0, SRCCOPY);
}
}
SelectObject(hdcMem, bmpfame);
BitBlt(hdc, 0, 0, 70, 40, hdcMem, 0, 0, SRCCOPY);
BitBlt(hdc, m_Map.mx * 10 - 20, 0, 40, 40, hdcMem, 80, 0, SRCCOPY);
BitBlt(hdc, m_Map.mx * 20 - 70, 0, 70, 40, hdcMem, 130, 0, SRCCOPY);
StretchBlt(hdc, 70, 0, m_Map.mx * 10 - 90, 40, hdcMem, 70, 0, 10, 40, SRCCOPY);
StretchBlt(hdc, m_Map.mx * 10 + 20, 0, m_Map.mx * 10 - 90, 40, hdcMem, 70, 0, 10, 40, SRCCOPY);
SelectObject(hdcMem, bmpface);
BitBlt(hdc, m_Map.mx * 10 - 12, 7, 24, 24, hdcMem, m_Map.winf * 24, 0, SRCCOPY);
SelectObject(hdcMem, bmpnub);
if (rm < 0) rm = 0;
BitBlt(hdc, 6, 5, 20, 28, hdcMem, rm / 100 * 20, 0, SRCCOPY);
if (rm >= 100) rm %= 100;
BitBlt(hdc, 26, 5, 20, 28, hdcMem, rm / 10 * 20, 0, SRCCOPY);
BitBlt(hdc, 46, 5, 20, 28, hdcMem, rm % 10 * 20, 0, SRCCOPY);
if (rt >= 1000) rt %= 1000;
BitBlt(hdc, m_Map.mx*20-66, 5, 20, 28, hdcMem, rt / 100 * 20, 0, SRCCOPY);
if (rt >= 100) rt %= 100;
BitBlt(hdc, m_Map.mx*20 - 46, 5, 20, 28, hdcMem, rt / 10 * 20, 0, SRCCOPY);
BitBlt(hdc, m_Map.mx*20 - 26, 5, 20, 28, hdcMem, rt % 10 * 20, 0, SRCCOPY);
DeleteDC(hdcMem);
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONUP:
ReleaseCapture();
nflag = 0;
if (!m_Map.winf || m_Map.winf == 2)
break;
if (m >= m_Map.mx || m < 0 || n >= m_Map.my || n < 0 || m_Map.Map[m][n] < 90)
break;
m_Map.LClinck(m, n, &rc);
if (m_Map.iswin())
m_Map.winf = 2;
if (m_M