📄 minedlg.cpp
字号:
// MineDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Mine.h"
#include "MineDlg.h"
#include "LED.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMineDlg dialog
CMineDlg::CMineDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMineDlg::IDD, pParent)
{
m_BarHeight = BARHEIGHT; //固定显示栏高度
m_MainButton = NULL;
//{{AFX_DATA_INIT(CMineDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); //可执行文件的“图标”
}
void CMineDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMineDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMineDlg, CDialog)
//{{AFX_MSG_MAP(CMineDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(ID_ABOUT, OnAbout)
ON_COMMAND(ID_FILE_LOW, OnFileLow)
ON_COMMAND(ID_FILE_MID, OnFileMid)
ON_COMMAND(ID_FILE_HIGH, OnFileHigh)
ON_COMMAND(ID_FILE_EXIT, OnFileExit)
ON_WM_CLOSE()
ON_COMMAND(ID_FILE_BEGIN, OnFileBegin)
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
ON_WM_LBUTTONDBLCLK()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMineDlg message handlers
BOOL CMineDlg::OnInitDialog()////////////////////////////////////////整个程序初始化
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//////////////////////////////////////////////////////////////
m_WideNum = DEFAULT_WIDTH; //初始化各变量
m_HighNum = DEFAULT_HEIGHT;
m_MineNum = DEFAULT_MINENUM;
InitVariables(); //确定变量
/////////////////////////////////////////////////////////////
CRect rc,clientrect;
GetWindowRect(rc); //得到整个扫雷窗的大小,窗口矩代入rc,客户区代入clientrect
GetClientRect(clientrect);
m_iFrameWidth = rc.Width()-clientrect.Width();//用整窗大小减去客户区大小得到外框大小
m_iFrameHeight = rc.Height()-clientrect.Height();
InitWindow(); //确定窗口(调整窗口大小)
m_MineCounter = NULL;
m_Timer = NULL;
InitControlBar(); //初始化显示区
/////////////////////////////////////////////////////////////
SetMine(); //放置雷
return TRUE; // return TRUE unless you set the focus to a control
}
void CMineDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal(); //显示about窗口
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMineDlg::OnPaint() // 相当与Document-View模式下的OnView()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
PaintClient(); //雷区显示
PaintControlBar();
IsGameFinished = AutoFinish();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMineDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMineDlg::OnAbout()
{
// TODO: Add your command handler code here
CAboutDlg Dlg;
Dlg.DoModal();
}
void CMineDlg::InitVariables() ////////////////////////////////////////设置各变量
{
m_ClientWidth = m_WideNum*16 + (m_WideNum-1); //客户区宽度
m_ClientHeight = m_HighNum*16 + (m_HighNum-1);//客户区高度
///////////////////////////////////////////////////////////////
if(m_SetState.GetSize()>0) //////////////////////////每次从新开始后都要清除矩阵原有数据
m_SetState.RemoveAll();
m_SetState.SetSize(m_WideNum*m_HighNum);//根据宽高数确定数组长度
if(m_OpenState.GetSize()>0)
m_OpenState.RemoveAll();
m_OpenState.SetSize(m_WideNum*m_HighNum);//根据宽高数确定数组长度
for(int i=0;i<m_HighNum;i++)
{
for(int j=0;j<m_WideNum;j++)
{
m_SetState[i*m_WideNum+j] = EMPTY;
m_OpenState[i*m_WideNum+j] = UNOPENED;
}
}
m_MineLeft = m_MineNum;
IsGameFinished = FALSE;
m_FirstBlick = FALSE;
}
void CMineDlg::InitWindow()
{
MoveWindow(300,200, //窗口左上角坐标
m_iFrameWidth+m_ClientWidth, //窗宽
m_iFrameHeight+m_BarHeight+m_ClientHeight); //窗高
CRect clientrect;
GetClientRect(clientrect);
m_MineRect.SetRect(CPoint(1,m_BarHeight),CPoint(clientrect.right,clientrect.bottom));//雷区
}
void CMineDlg::InitControlBar() ///////////////////////////////////////////设置显示区
{
/////////////////////////////////////////////////////////////////////
//显示区设置
if(m_MainButton != NULL)
delete m_MainButton;
if(m_MineCounter != NULL)
delete m_MineCounter;
if(m_Timer != NULL)
delete m_Timer;///////////////////类CLED中必须有Create()函数,在此才能顺利Delete
m_MainButton=new CBitmapButton(); //窗口中间的主控件
CRect rcClient,rc;
GetClientRect(rcClient);
rc.SetRect( CPoint((rcClient.Width()-BUTTONWIDE)/2,1),//将控件设置在中间
CPoint((rcClient.Width()+BUTTONWIDE)/2,BUTTONHEIGHT) );
m_MainButton->Create( NULL,
WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
//前两个Window Style属性为通常设置,
//如要在控件上贴位图必须有控件属性BS_OWNERDRAW
rc, //控件区域矩行
this,//父类指针,usually a CDialog
ID_FILE_BEGIN);//控件ID,将控件与菜单ID联系起来!!!!!!!!!!!!!!
m_MainButton->LoadBitmaps(IDB_NORMAL,IDB_DOWN);//两个Bitmap分别是up与down的状态
m_MainButton->Invalidate();
m_MineCounter = new CLED(this,3,m_MineNum,1002,CPoint(BITMAP_WIDE,1));
m_Timer = new CLED(this,3,0,1003,CPoint(rcClient.Width()-4*BITMAP_WIDE,1));
/*
You construct a child window in two steps. First, call the constructor,
which constructs the CWnd object. Then call Create, which creates the Windows
child window and attaches it to CWnd. Create initializes the window class name
and window name and registers values for its style, parent, and ID.
*/
SetTimer(1001,1000,NULL);//send a message per 1000 milliseconds设置时钟,且时钟标识为1001
}
void CMineDlg::SetMine() /////////////////////////////////////////////放置雷
{
int i,j,n;
//安地雷:
n=m_MineNum;
srand((unsigned)time(0)); // 产生随机数!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
while(n)
{
i=rand()%m_WideNum;
j=rand()%m_HighNum;
if(m_SetState[i*m_WideNum+j] != MINE)//判断,如此处无雷则放置雷(随机放置n个雷)
{
m_SetState[i*m_WideNum+j] = MINE;
n--;//设置一个雷后雷数减1
}
}
//计算地雷数:
for(i=0;i<m_HighNum;i++)
for(j=0;j<m_WideNum;j++)
{
if(m_SetState[i*m_WideNum+j] != MINE)
{
int count = 0;
for(int height=i-1; height<=i+1; height++)
for(int wide=j-1; wide<=j+1; wide++)
{
if((height>=0)&(height<m_HighNum)&(wide>=0)&(wide<m_WideNum))
{
if(m_SetState[height*m_WideNum+wide] == MINE)
count++;
}
}
if(count != 0)
m_SetState[i*m_WideNum+j] = count;
}
}
}
void CMineDlg::OnFileBegin()
{
// TODO: Add your command handler code here
InitVariables();
InitWindow();
InitControlBar();
SetMine();
PaintClient();
PaintControlBar();
}
void CMineDlg::OnFileLow()
{
// TODO: Add your command handler code here
m_WideNum = 9;
m_HighNum = 9;
m_MineNum = 10;
InitVariables();
InitWindow();
InitControlBar();
SetMine();
PaintClient();
PaintControlBar();
}
void CMineDlg::OnFileMid()
{
// TODO: Add your command handler code here
m_WideNum = 15;
m_HighNum = 15;
m_MineNum = 30;
InitVariables();
InitWindow();
InitControlBar();
SetMine();
PaintClient();
PaintControlBar();
}
void CMineDlg::OnFileHigh()
{
// TODO: Add your command handler code here
m_WideNum = 20;
m_HighNum = 20;
m_MineNum = 50;
InitVariables();
InitWindow();
InitControlBar();
SetMine();
PaintClient();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -