16,472
社区成员
发帖
与我相关
我的任务
分享
//*****************************************************************//
#include"stdafx.h"
#include <windows.h>
#include <stdio.h>
#include"mineswepper.h"
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
struct Block
{
bool ifOpened; //if the block has been clicked
bool ifMined; //if the block contains a mine
bool ifFlaged; //if user has flgged the block as a mine
int minesAround;
};
static Block blocks[20][20];
static bool startFlag=false;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
TCHAR szAppName[]=TEXT("Minesweeper");
TCHAR szClassName[]=TEXT("MinesweeperClassName");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc =WndProc;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hInstance =hInstance;
wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName =NULL;
wndclass.lpszClassName =szClassName;
RegisterClass(&wndclass);
hwnd=CreateWindow(szClassName,TEXT("Mineswepper"), WS_OVERLAPPEDWINDOW &~ WS_SIZEBOX,CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
RECT rect;
HDC hdc , hdcMem;
static HBITMAP hBitmap;
BITMAP bm;
static int bmX = 0;
static int bmY = 0;
static int mouseX;
static int mouseY;
static int row = 20;
static int coloum = 20;
switch (message)
{
case WM_CREATE:
{
for(int i=0;i<row;++i)
{
for(int l=0;l<coloum;++l)
{
blocks[i][l].ifMined=false;
blocks[i][l].ifFlaged=false;
blocks[i][l].ifOpened=false;
}
}
hBitmap=(HBITMAP)LoadImage(NULL,TEXT("Blocks.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
GetObject(hBitmap,sizeof(BITMAP),&bm);
bmX=bm.bmWidth;
bmY=bm.bmHeight;
return 0;
}
case WM_PAINT:
{
if(!startFlag)
{
hdc=BeginPaint(hwnd,&ps);
hdcMem=CreateCompatibleDC(hdc);
SelectObject(hdcMem,hBitmap);
for(int i=0;i<coloum;++i)
{
for(int l=0;l<row;++l)
{
BitBlt(hdc,(i*bmX),(l*bmY),bmX,bmY,hdcMem,0,0,SRCCOPY);
}
}
EndPaint(hwnd,&ps);
startFlag=true;
return 0;
}
for(int i=0;i<coloum;++i)
{
for(int l=0;l<row;++l)
{
if(blocks[i][l].ifOpened)
{
hdc=BeginPaint(hwnd,&ps);
hdcMem=CreateCompatibleDC(hdc);
SelectObject(hdcMem,hBitmap);
hBitmap=(HBITMAP)LoadImage(NULL,TEXT("mineKEYDOWN.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
GetObject(hBitmap,sizeof(BITMAP),&bm);
bmX=bm.bmWidth;
bmY=bm.bmHeight;
BitBlt(hdc,(i*bmX),(l*bmY),bmX,bmY,hdcMem,0,0,SRCCOPY);
EndPaint(hwnd,&ps);
}
}
}
return 0;
}
case WM_LBUTTONDOWN:
{
mouseX=LOWORD(lParam);
mouseY=HIWORD(lParam);
if( (mouseX<(coloum*bmX)) && (mouseY<(row*bmY)) )
{
int box_X = (mouseX/bmX)*bmX;
int box_Y = (mouseY/bmY)*bmY;
blocks[box_Y][box_X].ifOpened=true;
InvalidateRect(hwnd,NULL,true);
}
return 0;
}
case WM_DESTROY:
{
DeleteObject(hBitmap);
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd,message,wParam,lParam);
}