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

📄 mainfrm.cpp

📁 此程序是一个扫雷的小游戏
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "Saolei.h"

#include "MainFrm.h"
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
#include "afxmt.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int Timeflag=0;
CTime Time;
HBITMAP hBitmap0,hBitmap1,hBitmap2,hBitmap3,hBitmap4,hBitmap5,hBitmap6,hBitmap7,hBitmap8,hBitmap9,hBitmap10;
int RandomNumber[27][16];
HWND hwndChild[27][16],hwndChildClock,hwndChildTime,hwndChildGameStart;
typedef struct  NODE{
	int ID;
	NODE *next;
}node;
node* EmptyMineHead=0;
int   DigFinishMineSum=0;

typedef struct BUTTONHWND{
	HWND hwnd;
	BUTTONHWND* next;
}ButtonHwnd;
ButtonHwnd* ButtonHwndHead;
int LostFlag=0;
HWND hwndChildButton[8];





void SetMine();
void DoubleClickButtonDown(HWND hwnd);
void SetButtonMine();
void DoubleClickButtonUp(HWND hwnd);
void LButtonSelect2(HWND hwnd);
int GetSelectedButtonMineFlag(int ButtonID);
LRESULT CALLBACK ChildWndProcGameStart(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK ChildWndProcTime(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK ChildWndProcTime(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK ChildWndProcClock(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK ChildWndProcButton(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}


BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{	
	cs.lpszName=TEXT("扫雷");
	cs.cx=496;
	cs.cy=386;
	cs.style=WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX;
//	TRACE("PreCreateWindow\n");
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	WNDCLASS wndclass;
	wndclass.style=CS_HREDRAW|CS_VREDRAW;
	wndclass.lpfnWndProc=ChildWndProcButton;
	wndclass.cbClsExtra=0;
	wndclass.cbWndExtra=sizeof(long);
	wndclass.hInstance=AfxGetApp()->m_hInstance;
	wndclass.hIcon=0;
	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName=NULL;
	wndclass.lpszClassName=TEXT("Rect");
	RegisterClass(&wndclass);

	wndclass.lpfnWndProc=ChildWndProcClock;
	wndclass.cbWndExtra=sizeof(long);
	wndclass.hIcon=NULL;
	wndclass.lpszClassName=TEXT("Clock");
	RegisterClass(&wndclass);
	
	wndclass.lpfnWndProc=ChildWndProcTime;
	wndclass.cbWndExtra=sizeof(long);
	wndclass.hIcon=NULL;
	wndclass.lpszClassName=TEXT("Time");
	wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
	RegisterClass(&wndclass);

	wndclass.lpfnWndProc=ChildWndProcGameStart;
	wndclass.cbWndExtra=sizeof(long);
	wndclass.hIcon=NULL;
	wndclass.lpszClassName=TEXT("GameStart");
	RegisterClass(&wndclass);



	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

//	///////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

LRESULT CALLBACK ChildWndProcButton(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
//	TRACE("ChildWndProc \n ");
	HDC hdc,hdcMem;
	PAINTSTRUCT ps;	
	RECT rect;

	BITMAP bitmap;
	int flag=0,ButtonHwndFlag=0;
	static int lButtonDownFlag=0;
	static HWND Prehwnd;
	static UINT Premessage;
	static long  LButtonDown=0;
	static long  RButtonDown=0;
	ButtonHwnd* TempHwnd1,* TempHwnd2;
	int CycFlag=0;
	int ShowMineFlag=0;
    CTime t;

	CString strTemp;
	node* temp,*temp1;
	switch(message)
	{
		case WM_CREATE:
			SetWindowLong(hwnd,0,0);
			return 0;
		case WM_LBUTTONDOWN:
			SetWindowLong(hwndChildGameStart,0,1);
			InvalidateRect(hwndChildGameStart,NULL,TRUE);
			if(EmptyMineHead!=NULL){
				for(temp=EmptyMineHead;temp!=NULL;){
					temp1=temp->next;
					delete temp;
					temp=temp1;
				}
				EmptyMineHead=NULL;
			}
			LButtonDown=1;
			if(LButtonDown==1&&RButtonDown==1){
				DoubleClickButtonDown(hwnd);
 				return 0;
			}
			if(GetWindowLong(hwnd,0)!=40&&GetWindowLong(hwnd,0)!=41){
				lButtonDownFlag=1;
				Prehwnd=hwnd;
				Premessage=WM_LBUTTONDOWN;
				SetWindowLong(hwnd,0,1);
				InvalidateRect(hwnd,NULL,TRUE);
			}
			return 0;
		case WM_LBUTTONUP:
			SetWindowLong(hwndChildGameStart,0,0);
			InvalidateRect(hwndChildGameStart,NULL,TRUE);
			if(Timeflag==0) {
				Time=t.GetCurrentTime();
				Timeflag=1;
			}

			lButtonDownFlag=0;
			Prehwnd=0;
			Premessage=0;
			LButtonDown=0;
			if(RButtonDown==1){
				DoubleClickButtonUp(hwnd);
  				return 0;
			}
			if(GetWindowLong(hwnd,0)!=40&&GetWindowLong(hwnd,0)!=41){
 				if(GetSelectedButtonMineFlag(GetWindowLong(hwnd,GWL_ID))==8){

					SetWindowLong(hwnd,0,3);
					InvalidateRect(hwnd,NULL,TRUE);
					UpdateWindow(hwnd);
					for(int i=0;i<27;i++)
						for(int j=0;j<16;j++){
							DigFinishMineSum=0;
							SetWindowLong(hwndChildTime,0,DigFinishMineSum);
							InvalidateRect(hwndChildTime,NULL,TRUE);
							if(ButtonHwndHead!=NULL){
								for(TempHwnd1=ButtonHwndHead;TempHwnd1!=NULL;TempHwnd1=TempHwnd1->next){
									if(TempHwnd1->hwnd==hwndChild[i][j]) {ShowMineFlag=1;break;}
								}
							}
							if(ShowMineFlag==1){ ShowMineFlag=0;continue;}
							if(hwndChild[i][j]==hwnd){
								hwndChildButton[0]=hwnd;
								TempHwnd1=new ButtonHwnd;
								TempHwnd1->hwnd=hwndChild[i][j];
								TempHwnd1->next=NULL;
								ButtonHwndHead=TempHwnd1;
								hdc=GetDC(hwnd);
								GetObject(hBitmap10,sizeof(BITMAP),&bitmap);
								hdcMem=CreateCompatibleDC(hdc);
								SelectObject(hdcMem,hBitmap10);
								BitBlt(hdc,0,0,bitmap.bmWidth,bitmap.bmHeight,hdcMem,0,0,SRCCOPY);
								DeleteDC(hdcMem);
								continue;
							}
							if(GetSelectedButtonMineFlag(GetWindowLong(hwndChild[i][j],GWL_ID))==8){
								SetWindowLong(hwndChild[i][j],0,3);
							}
							else
								SetWindowLong(hwndChild[i][j],0,7);
							InvalidateRect(hwndChild[i][j],NULL,TRUE);
						}
					return 0;
				}
				if(GetSelectedButtonMineFlag(GetWindowLong(hwnd,GWL_ID))>0){
					SetWindowLong(hwnd,0,7);
					InvalidateRect(hwnd,NULL,TRUE);
					return 0;
				}
				SetWindowLong(hwnd,0,7);
				InvalidateRect(hwnd,NULL,TRUE);
				LButtonSelect2(hwnd);
			}
			return 0;
		case WM_RBUTTONDOWN:
			RButtonDown=1;
			if(LButtonDown==1&&RButtonDown==1){
    			DoubleClickButtonDown(hwnd);
				return 0;
			}	
			
			if(GetWindowLong(hwnd,0)!=40){
				if(GetWindowLong(hwnd,0)==-1){
					SetWindowLong(hwnd,0,0);
					InvalidateRect(hwnd,NULL,TRUE);
				}
				else if(GetWindowLong(hwnd,0)==41||GetWindowLong(hwnd,0)==2){
					SetWindowLong(hwnd,0,-1);
					InvalidateRect(hwnd,NULL,TRUE);
				}
				else {
					SetWindowLong(hwnd,0,2);
					InvalidateRect(hwnd,NULL,TRUE);
				}
			}
			return 0;
		case WM_RBUTTONUP:
			RButtonDown=0;
			if(LButtonDown==1){
				DoubleClickButtonUp(hwnd);
 				return 0;
			}
			return 0;
		case WM_MOUSEMOVE:	
			flag=GetWindowLong(hwnd,0);
			if(LButtonDown==1&&RButtonDown==1&&hwnd!=Prehwnd){
				DoubleClickButtonUp(Prehwnd);
				DoubleClickButtonDown(hwnd);
				Prehwnd=hwnd;
				Premessage=message;
			}
			else if(flag!=40&&flag!=41&&flag!=2&&flag!=-1){
				if(lButtonDownFlag==1&&hwnd!=Prehwnd){
					if(GetWindowLong(hwnd,0)!=40&&GetWindowLong(hwnd,0)!=-1){
						SetWindowLong(hwnd,0,1);	
						flag=1;
						InvalidateRect(hwnd,NULL,TRUE);
						UpdateWindow(hwnd);
						SetWindowLong(Prehwnd,0,0);
						InvalidateRect(Prehwnd,NULL,TRUE);
						Prehwnd=hwnd;
						Premessage=message;
					}		
				}
			}
			return 0;
		case WM_PAINT:
			hdc=BeginPaint(hwnd,&ps);
			GetClientRect(hwnd,&rect);
//			TRACE("rect.top=%d rect.left=%d rect.right=%d rect.bottom=%d\n",rect.top,rect.left,rect.right,rect.bottom);
			GetObject(hBitmap0,sizeof(BITMAP),&bitmap);
			hdcMem=CreateCompatibleDC(hdc);
			SelectObject(hdcMem,hBitmap0);
	       	BitBlt(hdc,0,0,bitmap.bmWidth,bitmap.bmHeight,hdcMem,0,0,SRCCOPY);
			DeleteDC(hdcMem);

			if(flag=GetWindowLong(hwnd,0))
			{
				if(flag==-1){
					GetObject(hBitmap8,sizeof(BITMAP),&bitmap);
					hdcMem=CreateCompatibleDC(hdc);
					SelectObject(hdcMem,hBitmap8);
					BitBlt(hdc,0,0,bitmap.bmWidth,bitmap.bmHeight,hdcMem,0,0,SRCCOPY);
					SetWindowLong(hwndChildTime,0,--DigFinishMineSum);
					InvalidateRect(hwndChildTime,NULL,TRUE);
					if(hwnd==ButtonHwndHead->hwnd){
						TempHwnd1=ButtonHwndHead;
						ButtonHwndHead=ButtonHwndHead->next;
						delete TempHwnd1;
					}
					else{						
						for(TempHwnd1=ButtonHwndHead;TempHwnd1!=NULL;TempHwnd1=TempHwnd1->next){
							if(hwnd==TempHwnd1->hwnd){
								TempHwnd2->next=TempHwnd1->next;
							    delete TempHwnd1;
								break;
							}
							TempHwnd2=TempHwnd1;
						}
					}
				}
				if(flag==1){
					GetObject(hBitmap1,sizeof(BITMAP),&bitmap);
					hdcMem=CreateCompatibleDC(hdc);
					SelectObject(hdcMem,hBitmap1);
					BitBlt(hdc,0,0,bitmap.bmWidth,bitmap.bmHeight,hdcMem,0,0,SRCCOPY);
				}
				if(flag==2||flag==41){
					GetObject(hBitmap2,sizeof(BITMAP),&bitmap);
					hdcMem=CreateCompatibleDC(hdc);
					SelectObject(hdcMem,hBitmap2);
					BitBlt(hdc,0,0,bitmap.bmWidth,bitmap.bmHeight,hdcMem,0,0,SRCCOPY);
					SetWindowLong(hwnd,0,41);
					if(ButtonHwndHead!=NULL){
						for(TempHwnd1=ButtonHwndHead;TempHwnd1!=NULL;TempHwnd1=TempHwnd1->next){
							if(hwnd==TempHwnd1->hwnd){
								ButtonHwndFlag=1; 
								break;
							}
						}
					}
					if(ButtonHwndFlag==0){
							TempHwnd1=new ButtonHwnd;
							TempHwnd1->hwnd=hwnd;
							TempHwnd1->next=ButtonHwndHead;
							ButtonHwndHead=TempHwnd1;

/*						TempHwnd1=new ButtonHwnd;
						TempHwnd1->hwnd=hwnd;
						if(ButtonHwndHead==NULL){
							ButtonHwndHead=TempHwnd1;
							ButtonHwndHead->next=NULL;
						}
						else{
							TempHwnd1->next=ButtonHwndHead;
					    	ButtonHwndHead=TempHwnd1;
						}*/
						SetWindowLong(hwndChildTime,0,++DigFinishMineSum);
						InvalidateRect(hwndChildTime,NULL,TRUE);
					}
					ButtonHwndFlag=0;
				}
				if(flag==3){
					GetObject(hBitmap3,sizeof(BITMAP),&bitmap);
					hdcMem=CreateCompatibleDC(hdc);
					SelectObject(hdcMem,hBitmap3);
					BitBlt(hdc,0,0,bitmap.bmWidth,bitmap.bmHeight,hdcMem,0,0,SRCCOPY);
					SetWindowLong(hwnd,0,40);
					SetWindowLong(hwndChildTime,0,++DigFinishMineSum);
					InvalidateRect(hwndChildTime,NULL,TRUE);
				}
				if(flag==7){
					GetObject(hBitmap7,sizeof(BITMAP),&bitmap);
					hdcMem=CreateCompatibleDC(hdc);
					SelectObject(hdcMem,hBitmap7);
					BitBlt(hdc,0,0,bitmap.bmWidth,bitmap.bmHeight,hdcMem,0,0,SRCCOPY);
            		SetBkMode(hdc,TRANSPARENT);
		    		strTemp.Format(TEXT("%d"),GetSelectedButtonMineFlag(GetWindowLong(hwnd,GWL_ID)));
					for(TempHwnd1=ButtonHwndHead;TempHwnd1!=NULL;TempHwnd1=TempHwnd1->next){
							if(hwnd==TempHwnd1->hwnd) { ButtonHwndFlag=1; break; }
					}
					if(ButtonHwndFlag==1&&GetSelectedButtonMineFlag(GetWindowLong(hwnd,GWL_ID))!=8){
			    			SetWindowLong(hwndChildTime,0,--DigFinishMineSum);
							InvalidateRect(hwndChildTime,NULL,TRUE);
					}

					if(GetSelectedButtonMineFlag(GetWindowLong(hwnd,GWL_ID))!=0&&
						GetSelectedButtonMineFlag(GetWindowLong(hwnd,GWL_ID))<=7)
						TextOut(hdc,5,1,strTemp,1);
					else if(GetSelectedButtonMineFlag(GetWindowLong(hwnd,GWL_ID))==8){

⌨️ 快捷键说明

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