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

📄 +

📁 用Visual C++实现的扫雷程序。基本功能都已实现。用到了很多VC的控件知识。
💻
字号:
// 扫雷View.cpp : implementation of the CMyView class
//

#include "stdafx.h"
#include "扫雷.h"

#include "扫雷Doc.h"
#include "扫雷View.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyView

IMPLEMENT_DYNCREATE(CMyView, CView)

BEGIN_MESSAGE_MAP(CMyView, CView)
	//{{AFX_MSG_MAP(CMyView)
	ON_COMMAND(IDM_NEWGAME, OnNewgame)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(IDM_ENDGAME, OnEndgame)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction

CMyView::CMyView()
{
	// TODO: add construction code here
    CMyApp *App=(CMyApp *)AfxGetApp();
	
}

CMyView::~CMyView()
{
}

BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
  
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyView drawing

void CMyView::OnDraw(CDC* pDC)
{
	CMyApp *App=(CMyApp *)AfxGetApp();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
    //得到位图
    CBitmap Bitmap;
    Bitmap.LoadBitmap(IDB_BITMAP);
    CDC dc;
	dc.CreateCompatibleDC(pDC);
    dc.SelectObject(&Bitmap);

	CBrush Brush;
	Brush.CreateSolidBrush(RGB(200,200,200));
	pDC->SelectObject(Brush);
	pDC->FillRect(CRect(0,0,App->Afx_Game.WindowRange.right,App->Afx_Game.WindowRange.bottom),&Brush);
   	if(App->Afx_Game.GameStart || App->Afx_Game.ActionNum!=0)
	{
	for(int x=0;x<=App->Afx_Game.Width*15;x+=15)
	{
		pDC->MoveTo(x,0);
		pDC->LineTo(x,App->Afx_Game.Heigh*15);
	}	
    for(int y=0;y<=App->Afx_Game.Heigh*15;y+=15)
	{
     pDC->MoveTo(0,y);
	 pDC->LineTo(App->Afx_Game.Width*15,y);
	}

	for(int i=0;i<App->Afx_Game.Width;i++)
	for(int j=0;j<App->Afx_Game.Heigh;j++)
	{
	if (App->Afx_Game.box[i][j].Action)
		pDC->BitBlt(i*15,j*15,15,15,&dc,App->Afx_Game.box[i][j].Pxy.x,App->Afx_Game.box[i][j].Pxy.y,SRCCOPY);
	else
	    pDC->BitBlt(i*15,j*15,15,15,&dc,15,0,SRCCOPY);
	}
	}
	pDC->DeleteDC();
    dc.DeleteDC();         // 删除内存句柄
	Bitmap.DeleteObject();


}

/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics

#ifdef _DEBUG
void CMyView::AssertValid() const
{
	CView::AssertValid();
}

void CMyView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
	return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers


void CMyView::OnNewgame() 
{
	// TODO: Add your command handler code here
	CMyApp *App=(CMyApp *)AfxGetApp();
	App->Afx_Game.InitGame();
	CMyView::SetTimer(IDT_GAMETIME,1000,NULL);
	CMyView::SetTimer(IDT_COUNT,100,NULL);
	char temp[10]="";
	sprintf(temp," %d ",App->Afx_Game.PlayLeftBombNum);
	App->Afx_Game.TitleBomb.SetWindowText(temp);
	App->Afx_Game.GameTime.SetWindowText(" 0");
	App->Afx_Game.GameEnd.SetWindowText("");
	CMenu *menu;
	menu=AfxGetMainWnd()->GetMenu();
    if (App->Afx_Game.BackWave)
	    menu->CheckMenuItem(IDM_BACKWAVE,MF_BYCOMMAND|MF_CHECKED);
	else 
	    menu->CheckMenuItem(IDM_BACKWAVE,MF_BYCOMMAND|MF_UNCHECKED);
	menu->EnableMenuItem(IDM_EASY,MF_BYCOMMAND|MF_GRAYED);
	menu->EnableMenuItem(IDM_NORMAL,MF_BYCOMMAND|MF_GRAYED);
	menu->EnableMenuItem(IDM_HARD,MF_BYCOMMAND|MF_GRAYED);
	menu->EnableMenuItem(IDM_DEFINE,MF_BYCOMMAND|MF_GRAYED);
	CMyView::Invalidate();
}


void CMyView::OnEndgame() 
{
	// TODO: Add your command handler code here
	CMyApp *App=(CMyApp *)AfxGetApp();
    App->Afx_Game.InitEndGame();
	CMyView::KillTimer(IDT_GAMETIME);
	CMyView::KillTimer(IDT_COUNT);
	App->Afx_Game.TitleBomb.SetWindowText("");
    App->Afx_Game.GameTime.SetWindowText("");
	App->Afx_Game.GameEnd.SetWindowText("");
	CMenu *menu;
	menu=AfxGetMainWnd()->GetMenu();
    if (App->Afx_Game.BackWave)
	    menu->CheckMenuItem(IDM_BACKWAVE,MF_BYCOMMAND|MF_CHECKED);
	else 
	    menu->CheckMenuItem(IDM_BACKWAVE,MF_BYCOMMAND|MF_UNCHECKED);
	menu->EnableMenuItem(IDM_EASY,MF_BYCOMMAND|MF_ENABLED);
	menu->EnableMenuItem(IDM_NORMAL,MF_BYCOMMAND|MF_ENABLED);
	menu->EnableMenuItem(IDM_HARD,MF_BYCOMMAND|MF_ENABLED);
	menu->EnableMenuItem(IDM_DEFINE,MF_BYCOMMAND|MF_ENABLED);
	CMyView::Invalidate();
}

void CMyView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    CMyApp *App=(CMyApp *)AfxGetApp();
	if (App->Afx_Game.GameStart)
	{
    if(App->Afx_Game.box[point.x/15][point.y/15].Action==FALSE)
	{
    App->Afx_Game.box[point.x/15][point.y/15].Action=TRUE;
	CClientDC dc(this);
	CDC *pDC;
	pDC=&dc;
	POINT XY;
    int Num=App->Afx_Game.GetDombNum(point);
	switch(Num)
	{
	case 0:
		XY.x=0;
		XY.y=0;
		break;
	case 1:
	    XY.x=15;
		XY.y=15;
		break;
	case 2:
	    XY.x=30;
		XY.y=15;
		break;
	case 3:
	    XY.x=45;
		XY.y=15;
		break;
	case 4:
	    XY.x=60;
		XY.y=15;
		break;
	case 5:
        XY.x=0;
		XY.y=30;
		break;
	case 6:
        XY.x=15;
		XY.y=30;
		break;
	case 7:
        XY.x=30;
		XY.y=30;
		break;
	case 8:
        XY.x=45;
		XY.y=30;
		break;
	default:
		break;
	}
	//游戏结束
    if (App->Afx_Game.box[point.x/15][point.y/15].Bomb)
	{
		XY.x=45;
		XY.y=0;
		App->Afx_Game.box[point.x/15][point.y/15].Pxy.x=XY.x;
	    App->Afx_Game.box[point.x/15][point.y/15].Pxy.y=XY.y;
		App->Afx_Game.GameStart=FALSE;
        App->Afx_Game.ActionNum++;
		App->Afx_Game.DrawAllBox(pDC);
	
	App->Afx_Game.GameEnd.SetWindowText("   负");
	CMenu *menu;
	menu=AfxGetMainWnd()->GetMenu();
	menu->CheckMenuItem(IDM_BACKWAVE,MF_BYCOMMAND|MF_UNCHECKED);
	menu->EnableMenuItem(IDM_EASY,MF_BYCOMMAND|MF_ENABLED);
	menu->EnableMenuItem(IDM_NORMAL,MF_BYCOMMAND|MF_ENABLED);
	menu->EnableMenuItem(IDM_HARD,MF_BYCOMMAND|MF_ENABLED);
	menu->EnableMenuItem(IDM_DEFINE,MF_BYCOMMAND|MF_ENABLED);
	}
	else
	{
    App->Afx_Game.ActionNum++;
	//过关
	if (App->Afx_Game.ActionNum==App->Afx_Game.Width*App->Afx_Game.Heigh)
	{
	App->Afx_Game.GameStart=FALSE;
	App->Afx_Game.GameEnd.SetWindowText("   胜");
    CMenu *menu;
	menu=AfxGetMainWnd()->GetMenu();
	menu->CheckMenuItem(IDM_BACKWAVE,MF_BYCOMMAND|MF_UNCHECKED);
	menu->EnableMenuItem(IDM_EASY,MF_BYCOMMAND|MF_ENABLED);
	menu->EnableMenuItem(IDM_NORMAL,MF_BYCOMMAND|MF_ENABLED);
	menu->EnableMenuItem(IDM_HARD,MF_BYCOMMAND|MF_ENABLED);
	menu->EnableMenuItem(IDM_DEFINE,MF_BYCOMMAND|MF_ENABLED);
	}

	//这里加入自绘过程(未行动并且无雷区)

    App->Afx_Game.ActionDrawBox(pDC,point.x/15,point.y/15);
	}
    App->Afx_Game.DrawBox(pDC,point,XY);
	//保存以便刷新
	App->Afx_Game.box[point.x/15][point.y/15].Pxy.x=XY.x;
	App->Afx_Game.box[point.x/15][point.y/15].Pxy.y=XY.y;
	}
	}
	CView::OnLButtonDown(nFlags, point);
}

void CMyView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call defaul
	CMyApp *App=(CMyApp *)AfxGetApp();
	if(App->Afx_Game.GameStart)
	{
	CClientDC dc(this);
	CDC *pDC;
	pDC=&dc;
	POINT XY;
	BOOL result=FALSE;
	if (App->Afx_Game.box[point.x/15][point.y/15].Tag && App->Afx_Game.ActionBombNum>=1 && App->Afx_Game.PlayLeftBombNum>=1)
	{
		result=TRUE;
		App->Afx_Game.ActionBombNum--;
		XY.x=15;
		XY.y=0;
        App->Afx_Game.ActionNum--;
		App->Afx_Game.PlayLeftBombNum++;
	    if (App->Afx_Game.box[point.x/15][point.y/15].Bomb)
			App->Afx_Game.LeftBombNum++;
        App->Afx_Game.DrawBox(pDC,point,XY);
	}
    
    if (App->Afx_Game.box[point.x/15][point.y/15].Action==FALSE) 
	{    
			result=TRUE;
        App->Afx_Game.ActionBombNum++;
    	XY.x=60;
	    XY.y=30;
        App->Afx_Game.ActionNum++;
	    App->Afx_Game.PlayLeftBombNum--;
		if (App->Afx_Game.box[point.x/15][point.y/15].Bomb)
			App->Afx_Game.LeftBombNum--;
		if (App->Afx_Game.LeftBombNum==0) 
		{
			App->Afx_Game.GameStart=FALSE;
	        App->Afx_Game.GameEnd.SetWindowText("   胜");
		    App->Afx_Game.DrawAllBox(pDC);
		}
		App->Afx_Game.DrawBox(pDC,point,XY);
	}	
	if(result)
	{
	char temp[10]="";
	sprintf(temp," %d ",App->Afx_Game.PlayLeftBombNum);
    App->Afx_Game.TitleBomb.SetWindowText(temp);	  
	App->Afx_Game.box[point.x/15][point.y/15].Tag=1-App->Afx_Game.box[point.x/15][point.y/15].Tag;
	App->Afx_Game.box[point.x/15][point.y/15].Action=1-App->Afx_Game.box[point.x/15][point.y/15].Action;
    App->Afx_Game.box[point.x/15][point.y/15].Pxy.x=XY.x;
	App->Afx_Game.box[point.x/15][point.y/15].Pxy.y=XY.y;
	}
	}
	CView::OnRButtonDown(nFlags, point);
}


void CMyView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CMyApp *App=(CMyApp *)AfxGetApp();
	switch(nIDEvent)
	{
	case IDT_COUNT:
	App->Afx_Game.Count++;
	if (App->Afx_Game.Count>60000)
		App->Afx_Game.Count=99;
	break;
	case IDT_GAMETIME:
		if(App->Afx_Game.GameStart)
		{
		if (App->Afx_Game.BackWave)
			Beep(500,50);
		(App->Afx_Game.TimeCount)++;
		if (App->Afx_Game.TimeCount>999) App->Afx_Game.TimeCount=0;
		char temp[10];
		sprintf(temp," %d",App->Afx_Game.TimeCount);
		App->Afx_Game.GameTime.SetWindowText(temp);
		}
		break;
	default:
		break;
	}
	CView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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