📄 wm.cpp
字号:
// Wm.cpp : Defines the entry point for the application.
#include "stdafx.h"
#include "resource.h"
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
HWND hWnd;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void init();
void load(char * filename);
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_WM, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WM);
// 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.
// COMMENTS:
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
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_WM);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
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
// COMMENTS:
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
srand((unsigned)time(NULL));
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, "拼图游戏", WS_OVERLAPPEDWINDOW,
100, 100, 400, 420, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
init(); //更新数据
SetTimer(hWnd,1,500,NULL);//设置计时器
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
// PURPOSE: Processes messages for the main window.
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
#define NEXT 10
//定义宽高
#define WIDTH 130
#define HEIGHT 130
//定义数组
int map [10][10];
//定义设备描述表
HDC memhdc;
//定义鼠标所在行列
int mouse_row,mouse_col;
int row9 = 2,col9 = 2;
//初始化数组
void init()
{
int s=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
s++;
map[i][j]=s;
}
}
//随机取两个格
int r1,c1,c2,r2;
int a;
int b;
for(int t=0;t<100;t++)
{
a = rand()%8+1;
b = rand()%8+1;
r1 = (a-1)/3;
c1 = (a-1)%3;
r2 = (b-1)/3;
c2 = (b-1)%3;
int m;
m=map[r1][c1];
map[r1][c1]=map[r2][c2];
map[r2][c2]=m;
}
//装载图片MAP1.bmp
char buf[100];
wsprintf(buf,"MAP1.bmp",1);
load(buf);
}
//从文件中装载图象
void load(char * filename)
{
HDC hdc = GetDC(hWnd);
memhdc = ::CreateCompatibleDC(hdc);
ReleaseDC(hWnd,hdc);
HBITMAP bmp=(HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
SelectObject(memhdc,bmp);
}
//转换到行列
void TurnToRowCol(int x,int y)
{
mouse_row = y / HEIGHT;
mouse_col = x / WIDTH;
if (mouse_row>2) mouse_row = 2;
if (mouse_col>2) mouse_col = 2;
}
//判断是否和空白块相邻
int isNear()
{
if (mouse_row == row9)
{
if((col9-mouse_col==1)||(col9-mouse_col==-1))
{
return true;
}
}
if (mouse_col==col9)
{
if ((mouse_row-row9==1)||(mouse_row-row9==-1))
{
return true;
}
}
return false;
}
//交换数据
void ExChange()
{
int m = map[mouse_row][mouse_col];
map[mouse_row][mouse_col] = map[row9][col9] ;
map[row9][col9] = m;
row9= mouse_row;
col9 = mouse_col;
}
//判断游戏结束
int isGameOver()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if (map[i][j] != (i*3+j+1))
{
return false;
}
}
return true;
}
//绘制游戏界面
void draw()
{
HDC hdc = GetDC(hWnd);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if (map[i][j]!=9)
{
int r,c;
r = (map[i][j]-1)/3;
c = (map[i][j]-1)%3;
BitBlt(hdc,j*WIDTH,i*HEIGHT,WIDTH,HEIGHT,memhdc,c*WIDTH,r*HEIGHT,SRCCOPY);
}
else
{
Rectangle(hdc,j*WIDTH,i*HEIGHT,(j+1)*WIDTH,(i+1)*HEIGHT);
}
}
ReleaseDC(hWnd,hdc);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
//当鼠标右键按下,拼图完成
case WM_RBUTTONDOWN:
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
map[i][j] =i*3+j+1;
col9=mouse_col;
row9=mouse_row;
}
draw();
}
break;
//当鼠标左键按下时
case WM_LBUTTONDOWN:
TurnToRowCol(LOWORD(lParam),HIWORD(lParam));
if (isNear())
{
//交换
ExChange();
//绘制
draw();
//当游戏结束的时候
if (isGameOver())
{
//显示游戏结束
if(MessageBox(hWnd,"拼图完成是否进入下一关","",MB_OKCANCEL|MB_ICONINFORMATION)==IDOK)
{
init();//更新数据
draw();//重新绘制
}
else
{
DestroyWindow(hWnd);//关闭窗口
}
}
}
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
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_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
draw();//绘制
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -