1. How to understand the whole structure of the project?
1.Cbutton.h 和 Cbutton.cpp文件:
Cbutton.h文件-
// Cbutton.h : main header file for the CBUTTON application
//
#if !defined(AFX_CBUTTON_H__240DD99D_BEDE_49BD_A960_3268C3644816__INCLUDED_)
#define AFX_CBUTTON_H__240DD99D_BEDE_49BD_A960_3268C3644816__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
/
// CCbuttonApp:
// See Cbutton.cpp for the implementation of this class
//
class CCbuttonApp : public CWinApp
{
public:
CCbuttonApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCbuttonApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CCbuttonApp)
afx_msg void OnAppAbout();
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CBUTTON_H__240DD99D_BEDE_49BD_A960_3268C3644816__INCLUDED_)
Cbutton.cpp文件-
// Cbutton.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "Cbutton.h"
#include "MainFrm.h"
#include "CbuttonDoc.h"
#include "CbuttonView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CCbuttonApp
BEGIN_MESSAGE_MAP(CCbuttonApp, CWinApp)
//{{AFX_MSG_MAP(CCbuttonApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
/
// CCbuttonApp construction
CCbuttonApp::CCbuttonApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/
// The one and only CCbuttonApp object
CCbuttonApp theApp;
/
// CCbuttonApp initialization
BOOL CCbuttonApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CCbuttonDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CCbuttonView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
/
// 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)
// No message handlers
//}}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()
// App command to run the dialog
void CCbuttonApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
/
// CCbuttonApp message handlers
analyze:
(1)the Cbutton.h only define a class CCbuttonApp which derived from the CWinApp class
### Cbutton.h:
- **Header Guards**: Ensures the header file is included only once.
- **Includes**: Standard and application-specific header files.
- **Class Declaration (`CCbuttonApp`)**:
- Derives from `CWinApp`.
- Declaration of constructor and `InitInstance()` function.
- Declaration of message map and command handlers.
- Declaration of the `OnAppAbout()` function.
### Cbutton.cpp:
- **Includes**: Standard and application-specific header files.
- **Macro Definitions (Debug Mode)**: Debugging macros for facilitating debugging.
- **Message Map**: Mapping of messages to member functions, including command handlers.
- **Constructor**: Definition of the `CCbuttonApp` constructor.
- **Instance Creation**: Creation of the one and only instance of `CCbuttonApp`.
- **Initialization (`InitInstance()`)**: Initialization logic for the application, including enabling control containers, registering document templates, and showing the main window.
- **About Dialog Implementation**: Definition and implementation of the `CAboutDlg` dialog class for displaying application information and the About command handler.
These files together define the behavior and structure of the `CCbuttonApp` application, including initialization, message handling, and user interface functionality.
2.CbuttonDoc.h 和 CbuttonDoc.cpp 文件:
--CubbtonDoc.h文件
// CbuttonDoc.h : interface of the CCbuttonDoc class
//
/
#if !defined(AFX_CBUTTONDOC_H__71C2E517_1911_4662_B46F_D2AADAF39F66__INCLUDED_)
#define AFX_CBUTTONDOC_H__71C2E517_1911_4662_B46F_D2AADAF39F66__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CCbuttonDoc : public CDocument
{
protected: // create from serialization only
CCbuttonDoc();
DECLARE_DYNCREATE(CCbuttonDoc)
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCbuttonDoc)
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CCbuttonDoc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CCbuttonDoc)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CBUTTONDOC_H__71C2E517_1911_4662_B46F_D2AADAF39F66__INCLUDED_)
-CbuttonDoc.cpp文件:
// CbuttonDoc.cpp : implementation of the CCbuttonDoc class
// Download by http://www.srcfans.com
#include "stdafx.h"
#include "Cbutton.h"
#include "CbuttonDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CCbuttonDoc
IMPLEMENT_DYNCREATE(CCbuttonDoc, CDocument)
BEGIN_MESSAGE_MAP(CCbuttonDoc, CDocument)
//{{AFX_MSG_MAP(CCbuttonDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CCbuttonDoc construction/destruction
CCbuttonDoc::CCbuttonDoc()
{
// TODO: add one-time construction code here
}
CCbuttonDoc::~CCbuttonDoc()
{
}
BOOL CCbuttonDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/
// CCbuttonDoc serialization
void CCbuttonDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/
// CCbuttonDoc diagnostics
#ifdef _DEBUG
void CCbuttonDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CCbuttonDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/
// CCbuttonDoc commands
analyze:
(1)the CbuttonDoc.h only declare named CCbuttonDoc and you can see some function of it
(2)in my opinion, there is nothing special here. THe .cpp file has so many TODO parts and it's a incomplete file which is to define a class CCbuttonDoc for this whole project .
so, let's go for the other meaningful codes
3.stdafx.h 和 stdafx.cpp:
.h 文件:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__42D6C997_624A_48A1_A8FE_B03A33D51058__INCLUDED_)
#define AFX_STDAFX_H__42D6C997_624A_48A1_A8FE_B03A33D51058__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__42D6C997_624A_48A1_A8FE_B03A33D51058__INCLUDED_)
.cpp 文件
// stdafx.cpp : source file that includes just the standard includes
// Cbutton.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
analyze:
(1)the .cpp file only include the .h file, so nothing meaningful of it
(2)and,the .h file is used to include commonly used system and project-specific header files that are used frequently throughout the project but are changed infrequently.
4. Resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by Cbutton.rc
//
#define IDD_ABOUTBOX 100
#define IDR_MAINFRAME 128
#define IDR_CBUTTOTYPE 129
#define IDB_BITMAP1 130
#define IDB_BITMAP_LEI 130
#define IDR_MENU1 133
#define IDB_BITMAP_QZ 134
#define IDB_REDMINE_BITMAP 140
#define ID_MENU_NEW 32771
#define ID_MENU_OUT 32772
#define ID_MENU_CAPTION 32773
#define ID_MACHINE 32774
#define IDB_BUTTON 61447
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_3D_CONTROLS 1
#define _APS_NEXT_RESOURCE_VALUE 141
#define _APS_NEXT_COMMAND_VALUE 32775
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
analyze:
(1) it said it was used by the Cbutton.rc file.
here is the definition of the .rc file
关于资源文件 - Win32 apps | Microsoft Learn
and if you want to learn all the things about the resource.h and .rc file , please read the article
Windows 资源文件(.rc文件)-CSDN博客
(2)after reading this article, I find that the macro define above just to replace some string to the integer for the other to use it
(3)let's to a look into the Cbutton.rc file: (open it with the text reader)
//Microsoft Developer Studio generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/
#undef APSTUDIO_READONLY_SYMBOLS
/
// Chinese (中国) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
#ifdef _WIN32
LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
#pragma code_page(936)
#endif //_WIN32
#ifdef APSTUDIO_INVOKED
/
//
// TEXTINCLUDE
//
1 TEXTINCLUDE DISCARDABLE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE DISCARDABLE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE DISCARDABLE
BEGIN
"#define _AFX_NO_SPLITTER_RESOURCES\r\n"
"#define _AFX_NO_OLE_RESOURCES\r\n"
"#define _AFX_NO_TRACKER_RESOURCES\r\n"
"#define _AFX_NO_PROPERTY_RESOURCES\r\n"
"\r\n"
"#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHT)\r\n"
"#ifdef _WIN32\r\n"
"LANGUAGE 4, 1\r\n"
"#pragma code_page(950)\r\n"
"#endif //_WIN32\r\n"
"#include ""res\\Cbutton.rc2"" // non-Microsoft Visual C++ edited resources\r\n"
"#include ""l.cht\\afxres.rc"" // Standard components\r\n"
"#include ""l.cht\\afxprint.rc"" // printing/print preview resources\r\n"
"#endif\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/
//
// Bitmap
//
IDB_BITMAP_LEI BITMAP DISCARDABLE "res\\bitmap1.bmp"
IDB_BITMAP_QZ BITMAP DISCARDABLE "res\\bitmap2.bmp"
IDR_MAINFRAME BITMAP MOVEABLE PURE "res\\Toolbar.bmp"
IDB_REDMINE_BITMAP BITMAP DISCARDABLE "res\\redmine.bmp"
/
//
// Menu
//
IDR_MENU1 MENU DISCARDABLE
BEGIN
POPUP "游戏"
BEGIN
MENUITEM "新游戏", ID_MENU_NEW
MENUITEM "自动扫雷", ID_MACHINE
MENUITEM SEPARATOR
MENUITEM "退出", ID_MENU_OUT
END
POPUP "帮助"
BEGIN
MENUITEM "游戏说明", ID_MENU_CAPTION
END
END
IDR_MAINFRAME MENU PRELOAD DISCARDABLE
BEGIN
POPUP "檔案(&F)"
BEGIN
MENUITEM "開啟新檔(&N)\tCtrl+N", ID_FILE_NEW
MENUITEM "開啟舊檔(&O)...\tCtrl+O", ID_FILE_OPEN
MENUITEM "儲存檔案(&S)\tCtrl+S", ID_FILE_SAVE
MENUITEM "另存新檔(&A)...", ID_FILE_SAVE_AS
MENUITEM SEPARATOR
MENUITEM "列印(&P)...\tCtrl+P", ID_FILE_PRINT
MENUITEM "預覽列印(&V)", ID_FILE_PRINT_PREVIEW
MENUITEM "列印設定(&R)...", ID_FILE_PRINT_SETUP
MENUITEM SEPARATOR
MENUITEM "最近開啟檔案", ID_FILE_MRU_FILE1, GRAYED
MENUITEM SEPARATOR
MENUITEM "結束(&X)", ID_APP_EXIT
END
POPUP "編輯(&E)"
BEGIN
MENUITEM "復原(&U)\tCtrl+Z", ID_EDIT_UNDO
MENUITEM SEPARATOR
MENUITEM "剪下(&T)\tCtrl+X", ID_EDIT_CUT
MENUITEM "複製(&C)\tCtrl+C", ID_EDIT_COPY
MENUITEM "貼上(&P)\tCtrl+V", ID_EDIT_PASTE
END
POPUP "檢視(&V)"
BEGIN
MENUITEM "工具列(&T)", ID_VIEW_TOOLBAR
MENUITEM "狀態列(&S)", ID_VIEW_STATUS_BAR
END
POPUP "說明(&H)"
BEGIN
MENUITEM "關於 Cbutton(&A)...", ID_APP_ABOUT
END
END
/
//
// Icon
//
// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDR_MAINFRAME ICON DISCARDABLE "res\\Cbutton.ico"
IDR_CBUTTOTYPE ICON DISCARDABLE "res\\CbuttonDoc.ico"
/
//
// Toolbar
//
IDR_MAINFRAME TOOLBAR DISCARDABLE 16, 15
BEGIN
BUTTON ID_FILE_NEW
BUTTON ID_FILE_OPEN
BUTTON ID_FILE_SAVE
SEPARATOR
BUTTON ID_EDIT_CUT
BUTTON ID_EDIT_COPY
BUTTON ID_EDIT_PASTE
SEPARATOR
BUTTON ID_FILE_PRINT
SEPARATOR
BUTTON ID_APP_ABOUT
END
/
//
// Accelerator
//
IDR_MAINFRAME ACCELERATORS PRELOAD MOVEABLE PURE
BEGIN
"N", ID_FILE_NEW, VIRTKEY, CONTROL
"O", ID_FILE_OPEN, VIRTKEY, CONTROL
"S", ID_FILE_SAVE, VIRTKEY, CONTROL
"P", ID_FILE_PRINT, VIRTKEY, CONTROL
"Z", ID_EDIT_UNDO, VIRTKEY, CONTROL
"X", ID_EDIT_CUT, VIRTKEY, CONTROL
"C", ID_EDIT_COPY, VIRTKEY, CONTROL
"V", ID_EDIT_PASTE, VIRTKEY, CONTROL
VK_BACK, ID_EDIT_UNDO, VIRTKEY, ALT
VK_DELETE, ID_EDIT_CUT, VIRTKEY, SHIFT
VK_INSERT, ID_EDIT_COPY, VIRTKEY, CONTROL
VK_INSERT, ID_EDIT_PASTE, VIRTKEY, SHIFT
VK_F6, ID_NEXT_PANE, VIRTKEY
VK_F6, ID_PREV_PANE, VIRTKEY, SHIFT
END
/
//
// Dialog
//
IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 235, 55
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "禸濘"
FONT 9, "新細明體"
BEGIN
ICON IDR_MAINFRAME,IDC_STATIC,11,17,20,20
LTEXT "Version 1.0",IDC_STATIC,40,10,119,8,SS_NOPREFIX
LTEXT "芞砉賜醱遺殤",IDC_STATIC,40,25,119,8
DEFPUSHBUTTON "?隅",IDOK,178,7,50,14,WS_GROUP
END
#ifndef _MAC
/
//
// Version
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,1
PRODUCTVERSION 1,0,0,1
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x4L
FILETYPE 0x1L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040404B0"
BEGIN
VALUE "CompanyName", "\0"
VALUE "FileDescription", "Cbutton MFC Application\0"
VALUE "FileVersion", "1, 0, 0, 1\0"
VALUE "InternalName", "Cbutton\0"
VALUE "LegalCopyright", "Copyright (C) 2010\0"
VALUE "LegalTrademarks", "\0"
VALUE "OriginalFilename", "Cbutton.EXE\0"
VALUE "ProductName", "Cbutton Application\0"
VALUE "ProductVersion", "1, 0, 0, 1\0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x404, 1200
END
END
#endif // !_MAC
/
//
// DESIGNINFO
//
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO DISCARDABLE
BEGIN
IDD_ABOUTBOX, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 228
TOPMARGIN, 7
BOTTOMMARGIN, 48
END
END
#endif // APSTUDIO_INVOKED
/
//
// String Table
//
STRINGTABLE PRELOAD DISCARDABLE
BEGIN
IDR_MAINFRAME "Cbutton\n\nCbutto\n\n\nCbutton.Document\nCbutto Document"
END
STRINGTABLE PRELOAD DISCARDABLE
BEGIN
AFX_IDS_APP_TITLE "Cbutton"
AFX_IDS_IDLEMESSAGE "就緒"
END
STRINGTABLE DISCARDABLE
BEGIN
ID_INDICATOR_EXT "EXT"
ID_INDICATOR_CAPS "CAP"
ID_INDICATOR_NUM "NUM"
ID_INDICATOR_SCRL "SCRL"
ID_INDICATOR_OVR "OVR"
ID_INDICATOR_REC "REC"
END
STRINGTABLE DISCARDABLE
BEGIN
ID_FILE_NEW "產生一新文件\n開啟新檔"
ID_FILE_OPEN "開啟一舊文件\n開啟舊檔"
ID_FILE_CLOSE "關閉目前的文件\n關閉"
ID_FILE_SAVE "儲存目前的文件\n儲存檔案"
ID_FILE_SAVE_AS "另外用一新名稱儲存目前的文件\n另存新檔"
ID_FILE_PAGE_SETUP "改變列印選項\n版面設定"
ID_FILE_PRINT_SETUP "改變印表機和列印選項\n列印設定"
ID_FILE_PRINT "列印目前的文件\n列印"
ID_FILE_PRINT_PREVIEW "顯示整頁\n預覽列印"
END
STRINGTABLE DISCARDABLE
BEGIN
ID_APP_ABOUT "顯示程式的資訊, 版本及版權\n關於"
ID_APP_EXIT "結束應用程式並提示儲存檔案\n結束"
END
STRINGTABLE DISCARDABLE
BEGIN
ID_FILE_MRU_FILE1 "開啟這個文件"
ID_FILE_MRU_FILE2 "開啟這個文件"
ID_FILE_MRU_FILE3 "開啟這個文件"
ID_FILE_MRU_FILE4 "開啟這個文件"
ID_FILE_MRU_FILE5 "開啟這個文件"
ID_FILE_MRU_FILE6 "開啟這個文件"
ID_FILE_MRU_FILE7 "開啟這個文件"
ID_FILE_MRU_FILE8 "開啟這個文件"
ID_FILE_MRU_FILE9 "開啟這個文件"
ID_FILE_MRU_FILE10 "開啟這個文件"
ID_FILE_MRU_FILE11 "開啟這個文件"
ID_FILE_MRU_FILE12 "開啟這個文件"
ID_FILE_MRU_FILE13 "開啟這個文件"
ID_FILE_MRU_FILE14 "開啟這個文件"
ID_FILE_MRU_FILE15 "開啟這個文件"
ID_FILE_MRU_FILE16 "開啟這個文件"
END
STRINGTABLE DISCARDABLE
BEGIN
ID_NEXT_PANE "切換到下一個窗格\n下一個窗格"
ID_PREV_PANE "切換到上一個窗格\n上一個窗格"
END
STRINGTABLE DISCARDABLE
BEGIN
ID_WINDOW_SPLIT "分割目前的視窗\n分割"
END
STRINGTABLE DISCARDABLE
BEGIN
ID_EDIT_CLEAR "刪除選取內容\n刪除"
ID_EDIT_CLEAR_ALL "全部刪除\n全部刪除"
ID_EDIT_COPY "複製選取內容到剪貼簿中\n複製"
ID_EDIT_CUT "剪下選取內容到剪貼簿中\n剪下"
ID_EDIT_FIND "尋找指定文字\n尋找"
ID_EDIT_PASTE "插入剪貼簿內容\n貼上"
ID_EDIT_REPEAT "重複前一動作\n重複"
ID_EDIT_REPLACE "以不同文字取代指定之文字\n取代"
ID_EDIT_SELECT_ALL "選取整篇文件\n選取全部"
ID_EDIT_UNDO "復原上一次動作\n復原"
ID_EDIT_REDO "重做前一個復原的動作\n重做"
END
STRINGTABLE DISCARDABLE
BEGIN
ID_VIEW_TOOLBAR "隱藏或顯示工具列\n切換工具列"
ID_VIEW_STATUS_BAR "隱藏或顯示狀態列\n切換狀態列"
END
STRINGTABLE DISCARDABLE
BEGIN
AFX_IDS_SCSIZE "改變視窗大小"
AFX_IDS_SCMOVE "改變視窗位置"
AFX_IDS_SCMINIMIZE "縮小視窗到一圖示"
AFX_IDS_SCMAXIMIZE "放大視窗到全螢幕"
AFX_IDS_SCNEXTWINDOW "切換到下一個文件視窗"
AFX_IDS_SCPREVWINDOW "切換到前一個文件視窗"
AFX_IDS_SCCLOSE "關閉工作中視窗並提示儲存文件"
END
STRINGTABLE DISCARDABLE
BEGIN
AFX_IDS_SCRESTORE "還原視窗至原來大小"
AFX_IDS_SCTASKLIST "叫出「工作列」"
END
STRINGTABLE DISCARDABLE
BEGIN
AFX_IDS_PREVIEW_CLOSE "關閉「預覽列印」模式\n取消預覽"
IDB_BUTTON "BUTTON"
END
STRINGTABLE DISCARDABLE
BEGIN
IDD_ABOUTBOX "Z"
END
#endif // Chinese (中国) resources
/
#ifndef APSTUDIO_INVOKED
/
//
// Generated from the TEXTINCLUDE 3 resource.
//
#define _AFX_NO_SPLITTER_RESOURCES
#define _AFX_NO_OLE_RESOURCES
#define _AFX_NO_TRACKER_RESOURCES
#define _AFX_NO_PROPERTY_RESOURCES
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHT)
#ifdef _WIN32
LANGUAGE 4, 1
#pragma code_page(950)
#endif //_WIN32
#include "res\Cbutton.rc2" // non-Microsoft Visual C++ edited resources
#include "l.cht\afxres.rc" // Standard components
#include "l.cht\afxprint.rc" // printing/print preview resources
#endif
/
#endif // not APSTUDIO_INVOKED
so, what do you think about this Cbutton.rc file:
(1)To summarize, the Cbutton.rc
file defines all the graphical and text resources used by the application, making it easier to manage and reference them in the project.
5. MainFrm.h 和 MainFrm.cpp file:
--MainFrm.h
// MainFrm.h : interface of the CMainFrame class
//
/
#if !defined(AFX_MAINFRM_H__DD9EC15F_BEDE_49B1_91C9_F39BB990D4C0__INCLUDED_)
#define AFX_MAINFRM_H__DD9EC15F_BEDE_49B1_91C9_F39BB990D4C0__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMainFrame : public CFrameWnd
{
protected: // create from serialization only
CMainFrame();
DECLARE_DYNCREATE(CMainFrame)
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMainFrame)
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
// Generated message map functions
protected:
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MAINFRM_H__DD9EC15F_BEDE_49B1_91C9_F39BB990D4C0__INCLUDED_)
--MainFrm.cpp:
// MainFrm.cpp : implementation of the CMainFrame class
// Download by http://www.srcfans.com
#include "stdafx.h"
#include "Cbutton.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
/* if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
*/
//MoveWindow(500,200,421,473);
SetMenu(NULL);
CMenu menu;
menu.LoadMenu(IDR_MENU1);
SetMenu(&menu);
menu.Detach();
CRect clientRect;
CRect windowRect;
GetClientRect(clientRect);
GetWindowRect(windowRect);
int cxNoClient = windowRect.Width() - clientRect.Width();
int cyNoClient = windowRect.Height() - clientRect.Height();
int cxWindow2 = 401 + cxNoClient;
int cyWindow2 = 401 + cyNoClient;
SetWindowPos(NULL, 0, 0, cxWindow2, cyWindow2, SWP_NOMOVE);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style=WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX;
m_strTitle = "课设版扫雷V5.0";
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
analyze:
(1) the MainFrm.h file only declare a class CMainFrm derived from CFrmWnd
(2) Overall, this MainFrm.cpp file sets up the main frame window of the application, including its size, style, and menu.
6.Cbuttonview.h and Cbuttonview.cpp file
--Cbuttonview.h:
// CbuttonView.h : interface of the CCbuttonView class
// Download by http://www.srcfans.com
/
#if !defined(AFX_CBUTTONVIEW_H__7E35BFD9_B8CA_41E4_ADBB_C878145E2260__INCLUDED_)
#define AFX_CBUTTONVIEW_H__7E35BFD9_B8CA_41E4_ADBB_C878145E2260__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CCbuttonView : public CView
{
protected: // create from serialization only
CCbuttonView();
DECLARE_DYNCREATE(CCbuttonView)
// Attributes
public:
CCbuttonDoc* GetDocument();
// Operations
public:
int RefreshGPanel(int GamePanel[30][30], int x,int y,int mine[30][30],int n,int m, int k);
void machine(int GamePanel[30][30], int n,int m, int &x,int &y);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCbuttonView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CCbuttonView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CCbuttonView)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnPaint();
afx_msg void OnCancelMode();
afx_msg void OnMenuCaption();
afx_msg void OnMenuNew();
afx_msg void OnMenuOut();
afx_msg void OnMachine();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
CButton cbutton[400][400];
int i,j;
int id;
int x,y;
CString str;
int i_lei[20];
int j_lei[20];
int mine[30][30];
int GamePanel[30][30];
int showmine;
int minenum;
int ifwin;
int current_i;
int current_j;
bool machinestart;
int clickcount;
};
#ifndef _DEBUG // debug version in CbuttonView.cpp
inline CCbuttonDoc* CCbuttonView::GetDocument()
{ return (CCbuttonDoc*)m_pDocument; }
#endif
/
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CBUTTONVIEW_H__7E35BFD9_B8CA_41E4_ADBB_C878145E2260__INCLUDED_)
--Cbuttonview.cpp:
#include "stdafx.h"
#include "Cbutton.h"
#include <queue>
#include "CbuttonDoc.h"
#include "CbuttonView.h"
#include<time.h>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define IDB_ZLH 0
/
// CCbuttonView
IMPLEMENT_DYNCREATE(CCbuttonView, CView)
BEGIN_MESSAGE_MAP(CCbuttonView, CView)
//{{AFX_MSG_MAP(CCbuttonView)
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_PAINT()
ON_WM_CANCELMODE()
ON_COMMAND(ID_MENU_CAPTION, OnMenuCaption)
ON_COMMAND(ID_MENU_NEW, OnMenuNew)
ON_COMMAND(ID_MENU_OUT, OnMenuOut)
ON_COMMAND(ID_MACHINE, OnMachine)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/*************************************以上代码不用管********************************************/
/******************************************************************************************************************/
/************************************************需要编写的两个函数如下********************************************/
int CCbuttonView::RefreshGPanel(int GamePanel[30][30], int x, int y, int mine[30][30], int n, int m, int k)
{
//请将你封装的RefreshGamePanel函数体添加到此处,并去掉下行的return 0
return 0;
}
void CCbuttonView::machine(int GamePanel[30][30], int n, int m, int &x, int &y)
{
//请将你实现的machine函数体添加到此处
}
/************************************************需要编写的两个函数如上********************************************/
/******************************************************************************************************************/
/*************************************以下代码不用管********************************************/
CCbuttonView::CCbuttonView()
{
// TODO: add construction code here
srand(int(time(0)));
minenum=50;
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
mine[i][j]=0;
GamePanel[i][j]=-1;
}
/*for(int k=0;k<minenum;k++)
{
i=rand()%20;
j=rand()%20;
while(mine[i][j]!=0)
{
i=rand()%20;
j=rand()%20;
}
mine[i][j]=1;
}*/
clickcount = 0;
x=0;y=0;
id=0;
i=0;j=0;
showmine=-1;
ifwin=0;
current_j=-1;
current_i=-1;
}
CCbuttonView::~CCbuttonView()
{
}
void CCbuttonView::OnMenuNew()
{
// TODO: Add your command handler code here
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
mine[i][j]=0;
GamePanel[i][j]=-1;
::ShowWindow(cbutton[i][j],SW_SHOW);
}
/*for(int k=0;k<minenum;k++)
{
i=rand()%20;
j=rand()%20;
while(mine[i][j]!=0)
{
i=rand()%20;
j=rand()%20;
}
mine[i][j]=1;
}*/
x=0;y=0;
id=0;
i=0;j=0;
ifwin=0;
clickcount = 0;
showmine=-1;
current_i=-1;
current_j=-1;
CRect rect1;
GetClientRect(&rect1);
InvalidateRect(rect1);
}
void CCbuttonView::OnMenuOut()
{
// TODO: Add your command handler code here
//PostQuitMessage(0);
exit(0) ;
}
BOOL CCbuttonView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/
// CCbuttonView drawing
void CCbuttonView::OnDraw(CDC* pDC)
{
CCbuttonDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/
// CCbuttonView printing
BOOL CCbuttonView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCbuttonView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCbuttonView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/
// CCbuttonView diagnostics
#ifdef _DEBUG
void CCbuttonView::AssertValid() const
{
CView::AssertValid();
}
void CCbuttonView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCbuttonDoc* CCbuttonView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCbuttonDoc)));
return (CCbuttonDoc*)m_pDocument;
}
#endif //_DEBUG
/
// CCbuttonView message handlers
int CCbuttonView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
id = 0;
x=0;y=0;
int realid;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
if (id == 0) realid = 1;
else realid = id;
cbutton[i][j].Create("",WS_CHILD | WS_VISIBLE,CRect(x,y,x+20,y+20),this,realid);
x+=20;
id+=20;
}
x=0;
y+=20;
}
return 0;
}
void CCbuttonView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnLButtonDown(nFlags, point);
}
void CCbuttonView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
/*for(int k=0;k<20;k++)
{
::ShowWindow(cbutton[i_lei[k]][j_lei[k]],SW_HIDE);
}*/
showmine*=-1;
CClientDC dc(this);
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP_LEI);
if(showmine==1)
{
CBrush brush(&bitmap);
//cbutton[0][0].SetBitmap();
//dc.FillRect(CRect(point.x,point.y,point.x+20,point.y+20),&brush);
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
if(mine[i][j]==1)
{
dc.FillRect(CRect(j*20,i*20,j*20+20,i*20+20),&brush);
}
}
}
}
else{
CRect rect1;
GetClientRect(&rect1);
InvalidateRect(rect1);
}
//cbutton[0][0].SetBitmap(bitmap);
CView::OnRButtonDown(nFlags, point);
}
void CCbuttonView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CBrush brush(RGB(255,255,255));
dc.SelectObject(&brush);
dc.FillRect(CRect(0,0,800,600),&brush);
CPen pen(PS_SOLID,1,RGB(180,180,180));
for( i=0;i<420;i+=20)
{
dc.SelectObject(&pen);
dc.MoveTo(i,0);
dc.LineTo(i,440);
dc.MoveTo(0,i);
dc.LineTo(400,i);
}
COLORREF col=dc.SetBkColor(RGB(255,255,255));
dc.SetTextColor(RGB(0,0,255));
if(ifwin==0)
{
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
if(GamePanel[i][j]==0)
::ShowWindow(cbutton[i][j],SW_HIDE);
else if(GamePanel[i][j]>0 && GamePanel[i][j]<9)
{
::ShowWindow(cbutton[i][j],SW_HIDE);
str.Format("%d",GamePanel[i][j]);
dc.SetTextColor(RGB(0,0,255));
dc.DrawText(str,CRect(j*20,i*20,j*20+20,i*20+20),DT_CENTER);
}
}
}
else if(ifwin==-1 || ifwin==1)
{
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
if(GamePanel[i][j]==0)
::ShowWindow(cbutton[i][j],SW_HIDE);
else if(GamePanel[i][j]>0 && GamePanel[i][j]<9)
{
::ShowWindow(cbutton[i][j],SW_HIDE);
str.Format("%d",GamePanel[i][j]);
dc.SetTextColor(RGB(0,0,255));
dc.DrawText(str,CRect(j*20,i*20,j*20+20,i*20+20),DT_CENTER);
}
CBitmap bitmap,bitmap1;
bitmap.LoadBitmap(IDB_BITMAP_LEI);
CBrush brush(&bitmap);
if(mine[i][j]==1)
{
::ShowWindow(cbutton[i][j],SW_HIDE);
if(ifwin==-1 && current_i==i && current_j==j)
{
bitmap1.LoadBitmap(IDB_REDMINE_BITMAP);
CBrush brush1(&bitmap1);
dc.FillRect(CRect(j*20,i*20,j*20+20,i*20+20),&brush1);
}
else dc.FillRect(CRect(j*20,i*20,j*20+20,i*20+20),&brush);
}
}
}
// Do not call CView::OnPaint() for painting messages
}
void CCbuttonView::OnCancelMode()
{
CView::OnCancelMode();
// TODO: Add your message handler code here
}
void CCbuttonView::OnMenuCaption()
{
// TODO: Add your command handler code here
MessageBox("简易版扫雷游戏图形界面框架","扫雷");
}
BOOL CCbuttonView::OnCommand(WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
int ii,jj,k,ButtonCmdId=LOWORD(wParam);
//CString string;
//string.Format("%d",ButtonCmdId);
//MessageBox(string);
if(ButtonCmdId==1)
{
ii = 0;
jj = 0;
}
else
{
ii = (ButtonCmdId / 400);
jj = (ButtonCmdId - 400 * ii) / 20;
}
//string.Format("%d,%d", ii, jj);
//MessageBox(string);
if (clickcount == 0)
{
for (int k = 0; k < minenum; k++)
{
i = rand() % 20;
j = rand() % 20;
while ( (i==ii && j==jj) || mine[i][j] != 0)
{
i = rand() % 20;
j = rand() % 20;
}
mine[i][j] = 1;
}
}
clickcount++;
/********************************************************************/
/***/ ifwin=RefreshGPanel(GamePanel,ii,jj,mine,20,20,minenum); /***/
/********************************************************************/
CRect rect1;
GetClientRect(&rect1);
current_j=jj;
current_i=ii;
if(ifwin==-1 || ifwin==1)
{
InvalidateRect(rect1);
if(ifwin==-1)
MessageBox("你输了!点击确定重新开始","扫雷",MB_ICONEXCLAMATION);
else MessageBox("恭喜,你赢了!点击确定重新开始","扫雷",MB_ICONINFORMATION);
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
mine[i][j]=0;
GamePanel[i][j]=-1;
::ShowWindow(cbutton[i][j],SW_SHOW);
}
/*for(int k=0;k<minenum;k++)
{
i=rand()%20;
j=rand()%20;
while(mine[i][j]!=0)
{
i=rand()%20;
j=rand()%20;
}
mine[i][j]=1;
}*/
clickcount = 0;
x=0;y=0;
id=0;
i=0;j=0;
ifwin=0;
showmine=-1;
current_i=-1;
current_j=-1;
machinestart=false;
}
//InvalidateRect(rect1);
{
CClientDC dc(this); // device context for painting
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
if(GamePanel[i][j]==0)
::ShowWindow(cbutton[i][j],SW_HIDE);
else if(GamePanel[i][j]>0 && GamePanel[i][j]<9)
{
::ShowWindow(cbutton[i][j],SW_HIDE);
str.Format("%d",GamePanel[i][j]);
dc.SetTextColor(RGB(0,0,255));
dc.DrawText(str,CRect(j*20,i*20,j*20+20,i*20+20),DT_CENTER);
}
}
}
return CView::OnCommand(wParam, lParam);
}
void CCbuttonView::OnMachine()
{
// TODO: Add your command handler code here
int x = -1, y = -1;
machinestart = true;
int tim = 50, count = 0;
while (machinestart && count<400)
{
/****************************************************/
/*********/machine(GamePanel, 20, 20, x, y);/********/
/****************************************************/
if (x<0 || x>19 || y<0 || y>19) break;
count++;
SendMessage(WM_COMMAND, MAKEWPARAM(x * 400 + 20 * y, BN_CLICKED), 0);
Sleep(tim);
}
}
analyze:
(1)Overall, this .h file defines the interface and behavior of the CCbuttonView
class, including its methods for drawing, handling events, and interacting with the document.
(2)Overall, the .cpp file contains the implementation of event handlers, initialization functions, and placeholders for game logic functions.
absolutely, these two file are the main file of this project. So, please understand it totally.
why not get more details about this .cpp file:
--onMachine function:
void CCbuttonView::OnMachine()
{
// TODO: Add your command handler code here
int x = -1, y = -1;
machinestart = true;
int tim = 50, count = 0;
while (machinestart && count<400)
{
/****************************************************/
/*********/machine(GamePanel, 20, 20, x, y);/********/
/****************************************************/
if (x<0 || x>19 || y<0 || y>19) break;
count++;
SendMessage(WM_COMMAND, MAKEWPARAM(x * 400 + 20 * y, BN_CLICKED), 0);
Sleep(tim);
}
}
Overall, this function controls the automated gameplay of the machine player by repeatedly calculating the next move, sending click events to the view, and introducing delays between moves.
especailly, how to understand this line of code
SendMessage(WM_COMMAND, MAKEWPARAM(x * 400 + 20 * y, BN_CLICKED), 0);
//especially this line below,you know,we only has x=0-19,y=0-19 selections,and
//this strategy make the two-dimension(x,y) to one-dimension x*400+20*y,and you can seperate the (x,y)
//from the x*400+y*20 at all and the BN_ClICKED is macro with value 0 and put on high-order
In summary, this line of code sends a WM_COMMAND
message to the window associated with the CCbuttonView
class, indicating that a button has been clicked. The x * 400 + 20 * y
part represents the control identifier (likely button coordinates), and BN_CLICKED
indicates that the button has been clicked.
--onCommand():
BOOL CCbuttonView::OnCommand(WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
int ii,jj,k,ButtonCmdId=LOWORD(wParam);
//CString string;
//string.Format("%d",ButtonCmdId);
//MessageBox(string);
if(ButtonCmdId==1)
{
ii = 0;
jj = 0;
}
else
{
ii = (ButtonCmdId / 400);
jj = (ButtonCmdId - 400 * ii) / 20;
}
//string.Format("%d,%d", ii, jj);
//MessageBox(string);
if (clickcount == 0)
{
for (int k = 0; k < minenum; k++)
{
i = rand() % 20;
j = rand() % 20;
while ( (i==ii && j==jj) || mine[i][j] != 0)
{
i = rand() % 20;
j = rand() % 20;
}
mine[i][j] = 1;
}
}
clickcount++;
/********************************************************************/
/***/ ifwin=RefreshGPanel(GamePanel,ii,jj,mine,20,20,minenum); /***/
/********************************************************************/
CRect rect1;
GetClientRect(&rect1);
current_j=jj;
current_i=ii;
if(ifwin==-1 || ifwin==1)
{
InvalidateRect(rect1);
if(ifwin==-1)
MessageBox("你输了!点击确定重新开始","扫雷",MB_ICONEXCLAMATION);
else MessageBox("恭喜,你赢了!点击确定重新开始","扫雷",MB_ICONINFORMATION);
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
mine[i][j]=0;
GamePanel[i][j]=-1;
::ShowWindow(cbutton[i][j],SW_SHOW);
}
/*for(int k=0;k<minenum;k++)
{
i=rand()%20;
j=rand()%20;
while(mine[i][j]!=0)
{
i=rand()%20;
j=rand()%20;
}
mine[i][j]=1;
}*/
// the code snippet below are
clickcount = 0;
x=0;y=0;
id=0;
i=0;j=0;
ifwin=0;
showmine=-1;
current_i=-1;
current_j=-1;
machinestart=false;
}
//InvalidateRect(rect1);
{
CClientDC dc(this); // device context for painting
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
if(GamePanel[i][j]==0)
::ShowWindow(cbutton[i][j],SW_HIDE);
else if(GamePanel[i][j]>0 && GamePanel[i][j]<9)
{
::ShowWindow(cbutton[i][j],SW_HIDE);
str.Format("%d",GamePanel[i][j]);
dc.SetTextColor(RGB(0,0,255));
dc.DrawText(str,CRect(j*20,i*20,j*20+20,i*20+20),DT_CENTER);
}
}
}
return CView::OnCommand(wParam, lParam);
}
let's go into this func:
Overall, this function manages the game logic and user interaction, updating the game state based on button clicks and handling win/loss conditions.
especially,let's pay attention to these parts:
--first—— the rectangle for the window
CRect rect1;
GetClientRect(&rect1);
current_j=jj;
current_i=ii;
create a rectangle object,
GetClientRect(&rect1);
: This line calls the GetClientRect
function to retrieve the coordinates of the client area of the window and stores them in the rect1
object. The client area is the area inside the window's borders, excluding the borders, menu bar, and scroll bars.
--second invalidate the area
InvalidateRect(rect1);
Overall, InvalidateRect(rect1)
is used to request a repaint of the entire client area of the window, which ensures that any changes made to the window's content are visually reflected to the user.
--third MessageBox function , and display so infomation to the client and a choice to restart
if(ifwin==-1)
MessageBox("你输了!点击确定重新开始","扫雷",MB_ICONEXCLAMATION);
else MessageBox("恭喜,你赢了!点击确定重新开始","扫雷",MB_ICONINFORMATION);
In summary, the MessageBox
function is used in this code to provide feedback to the user based on the outcome of the game (win or loss). It informs the user about the result and prompts them to click "OK" to restart the game. Additionally, it uses different icons (exclamation mark for loss and information icon for win) to visually convey the message type.
--fourth the ::ShowWindow() function here
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{ //initiate the all mine place and all GamePanel place
mine[i][j]=0;
GamePanel[i][j]=-1;
::ShowWindow(cbutton[i][j],SW_SHOW);
}
Syntax: ::ShowWindow(hWnd, nCmdShow)
hWnd
: Handle to the window to be shown.nCmdShow
: Specifies how the window is to be shown. This parameter can be one of the following values:
SW_SHOW
: Activates the window and displays it in its current size and position.
SW_HIDE
: Hides the window and activates another window.
In the provided code, ::ShowWindow(cbutton[i][j], SW_SHOW)
is used to show the window associated with the button cbutton[i][j]
. It's likely that these buttons represent some elements of the game interface, and this line ensures that they are initially displayed when the game starts. The SW_SHOW
flag indicates that the window should be shown in its current size and position.
--fifth draw every (i,j) whether with a number or just hide the button
{
CClientDC dc(this); // device context for painting
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
if(GamePanel[i][j]==0)
::ShowWindow(cbutton[i][j],SW_HIDE);
else if(GamePanel[i][j]>0 && GamePanel[i][j]<9)
{
::ShowWindow(cbutton[i][j],SW_HIDE);
str.Format("%d",GamePanel[i][j]);
dc.SetTextColor(RGB(0,0,255));
dc.DrawText(str,CRect(j*20,i*20,j*20+20,i*20+20),DT_CENTER);
}
}
}
By executing this code snippet, the user interface is updated to reflect the content of the GamePanel
array, showing numbers for cells containing adjacent mines counts and hiding buttons for cells with no content.
let's go more exactly:
CClientDC dc(this);
By creating a CClientDC
object in this way, the code can perform drawing operations, such as displaying text or shapes, on the window's client area. The device context (dc
) provides methods and properties for drawing operations, allowing the code to update the visual appearance of the window dynamically.
str.Format("%d",GamePanel[i][j]);
dc.SetTextColor(RGB(0,0,255));
dc.DrawText(str,CRect(j*20,i*20,j*20+20,i*20+20),DT_CENTER);
set the number to string, and set the color to blue, then draw the text on the(i,j)rectangle centerly
--onMenuCaption() func:
void CCbuttonView::OnMenuCaption()
{
// TODO: Add your command handler code here
MessageBox("简易版扫雷游戏图形界面框架","扫雷");
}
So, when the menu item "Caption" is selected, it shows a message box displaying information about the Minesweeper game graphical interface framework.
--OnPaint() function
void CCbuttonView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CBrush brush(RGB(255,255,255));
dc.SelectObject(&brush);
dc.FillRect(CRect(0,0,800,600),&brush);
CPen pen(PS_SOLID,1,RGB(180,180,180));
for( i=0;i<420;i+=20)
{
dc.SelectObject(&pen);
dc.MoveTo(i,0);
dc.LineTo(i,440);
dc.MoveTo(0,i);
dc.LineTo(400,i);
}
COLORREF col=dc.SetBkColor(RGB(255,255,255));
dc.SetTextColor(RGB(0,0,255));
if(ifwin==0)
{
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
if(GamePanel[i][j]==0)
::ShowWindow(cbutton[i][j],SW_HIDE);
else if(GamePanel[i][j]>0 && GamePanel[i][j]<9)
{
::ShowWindow(cbutton[i][j],SW_HIDE);
str.Format("%d",GamePanel[i][j]);
dc.SetTextColor(RGB(0,0,255));
dc.DrawText(str,CRect(j*20,i*20,j*20+20,i*20+20),DT_CENTER);
}
}
}
else if(ifwin==-1 || ifwin==1)
{
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
if(GamePanel[i][j]==0)
::ShowWindow(cbutton[i][j],SW_HIDE);
else if(GamePanel[i][j]>0 && GamePanel[i][j]<9)
{
::ShowWindow(cbutton[i][j],SW_HIDE);
str.Format("%d",GamePanel[i][j]);
dc.SetTextColor(RGB(0,0,255));
dc.DrawText(str,CRect(j*20,i*20,j*20+20,i*20+20),DT_CENTER);
}
CBitmap bitmap,bitmap1;
bitmap.LoadBitmap(IDB_BITMAP_LEI);
CBrush brush(&bitmap);
if(mine[i][j]==1)
{
::ShowWindow(cbutton[i][j],SW_HIDE);
if(ifwin==-1 && current_i==i && current_j==j)
{
bitmap1.LoadBitmap(IDB_REDMINE_BITMAP);
CBrush brush1(&bitmap1);
dc.FillRect(CRect(j*20,i*20,j*20+20,i*20+20),&brush1);
}
else dc.FillRect(CRect(j*20,i*20,j*20+20,i*20+20),&brush);
}
}
}
// Do not call CView::OnPaint() for painting messages
}
Overall, this function manages the visual representation of the game board, including drawing the background, grid lines, numbers, and bitmaps based on the game state.
and it also draw the mine in the end of the game
--about the pen object
CPen pen(PS_SOLID,1,RGB(180,180,180));
Overall, the CPen
class provides a convenient interface for working with pens in MFC applications, allowing developers to create and manipulate pens for various drawing tasks.
and for this line of code:
the line of code declares a CPen
object named pen
with a solid style, a width of 1 pixel, and a light gray color. This pen can then be used for drawing operations, such as lines or curves, in the specified color and style.
--about the brush class
CBrush brush(RGB(255,255,255));
Overall, the CBrush
class encapsulates various types of brushes used for drawing operations in MFC applications. It provides constructors and creation functions to create different types of brushes, along with methods to retrieve information about brushes and handle resources.
and this line of code create a pure white brush for the drawing
--about CBitmap class
CBitmap bitmap,bitmap1;
Overall, the CBitmap
class provides a convenient interface for working with bitmap images in MFC applications, including loading, creating, and manipulating bitmap resources.
In the context of Microsoft Foundation Class (MFC) programming, CBitmap
is a class that represents a Windows bitmap object. It encapsulates functionality for working with bitmap images within MFC-based applications.
--OnRButtonDown() function
void CCbuttonView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
/*for(int k=0;k<20;k++)
{
::ShowWindow(cbutton[i_lei[k]][j_lei[k]],SW_HIDE);
}*/
showmine*=-1;
CClientDC dc(this);
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP_LEI);
if(showmine==1)
{
CBrush brush(&bitmap);
//cbutton[0][0].SetBitmap();
//dc.FillRect(CRect(point.x,point.y,point.x+20,point.y+20),&brush);
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
if(mine[i][j]==1)
{
dc.FillRect(CRect(j*20,i*20,j*20+20,i*20+20),&brush);
}
}
}
}
else{
CRect rect1;
GetClientRect(&rect1);
InvalidateRect(rect1);
}
//cbutton[0][0].SetBitmap(bitmap);
CView::OnRButtonDown(nFlags, point);
}
In summary, this function allows the user to toggle the visibility of mines on the game board by right-clicking within the view. When the user right-clicks, it either shows or hides the mines based on the current state of the showmine
variable.
--OnLButtonDown() function
void CCbuttonView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnLButtonDown(nFlags, point);
}
In the default handling, the framework processes the left mouse button down event according to its default behavior, such as updating the mouse state and possibly initiating a drag-and-drop operation or other interactions, depending on the context and the current state of the application.
so, it just call the base func in base class CView
--OnCreate() function
int CCbuttonView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1) //if the base class return -1 ,then this func return -1
return -1;
// TODO: Add your specialized creation code here
id = 0;
x=0;y=0;
int realid;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
if (id == 0) realid = 1; // the first realid will be 1
else realid = id;
cbutton[i][j].Create("",WS_CHILD | WS_VISIBLE,CRect(x,y,x+20,y+20),this,realid);
x+=20;
id+=20;
}
x=0;
y+=20;
}
return 0;
}
Overall, this function is responsible for setting up and creating a grid of buttons (20x20) within the view. Each button is given a unique identifier and positioned accordingly within the grid.
cbutton[i][j].Create("",WS_CHILD | WS_VISIBLE,CRect(x,y,x+20,y+20),this,realid);
.Create("", WS_CHILD | WS_VISIBLE, CRect(x, y, x + 20, y + 20), this, realid)
: This part calls the Create
method of the CButton
object. This method is used to create a new button control.
--OnMenuNew() function
void CCbuttonView::OnMenuNew()
{
// TODO: Add your command handler code here
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
mine[i][j]=0;
GamePanel[i][j]=-1;
::ShowWindow(cbutton[i][j],SW_SHOW);
}
/*for(int k=0;k<minenum;k++)
{
i=rand()%20;
j=rand()%20;
while(mine[i][j]!=0)
{
i=rand()%20;
j=rand()%20;
}
mine[i][j]=1;
}*/
x=0;y=0;
id=0;
i=0;j=0;
ifwin=0;
clickcount = 0;
showmine=-1;
current_i=-1;
current_j=-1;
CRect rect1;
GetClientRect(&rect1);
InvalidateRect(rect1);
}
Finally, the function invalidates the entire client area (InvalidateRect(rect1)
), which means that the entire window will be marked as needing to be redrawn. This typically triggers a call to the OnPaint()
function to redraw the window contents.
--and other function just so so......
2.Review more generally about the whole project to generate a readme
1.the resource.h define some macro which will be used in the other function
2.the .rc file include the resource.h file and define more macros here ,the Cbutton.rc
file defines all the graphical and text resources used by the application, making it easier to manage and reference them in the project.so it's only used to make it easier to refer them in the project
3.the Frm.cpp defines the windows size and the menu size and something like it
4.and finally —— it's our CbuttonView.cpp file, this is the main file of the application, let's break down the whole file structure
(1)Message Map:The message map allows the framework to route messages sent to instances of CCbuttonView
to the appropriate member functions for handling. It establishes a connection between Windows messages or user commands and the corresponding member functions in the class.
(2)Automatic mine sweeper strategy func:this part input the current GPanel and output the next position (x,y) to strike
(3)RefreshGPanel and Machine part : this part get the (x,y) and refresh the GPanel, determine whether the game is over.
(4)initiate the GPanel and draw the base of it: the Construtor of the class and the OnMenuNew func
(5)strange part ??? I don't really understand yet:like draw,prepare ... the previous work's author try to rewrite some func in the afxwin.h but something haven't done yet
(6)OnCreate part: create the id of the 20*20 pixel button
(7)Left button and Right button part : the left click only use the base func enough, and the right click let the client reveal all the mine which is a cheating way
(8)Ondraw part: this part use the pen and brush object, and draw the line of the row and col ,draw the block and number , if the game end it also draw the mine
(9)OnCommand func part: a very important func, it get the index of the click pixel as input, and calculate the clickcount here ,and get the ifwin flag from the RefreshGPanel func then MessageBox to deliver the win or loss infomation with refresh the whole panel . Also , it show the GPanel again. Fianlly, it call the base OnCommand func to execute something must be execute
(10)On Machine part (I see it as a entry of the whole project): it calls the machine func that define the strategy to choose the index of the next pixel chosen by automatically
3. How to run this project with GUI?
1. I have a classmate who had already run this GUI project successfully and our TA has run too,so I will consult them if necessary.
(1) our classmate's video show: (I just borrowed this video from him)
【中山大学本科生C++大作业】一个很简陋的自动扫雷QWQ_哔哩哔哩_bilibili
2.Just get the project from my github and run the .sln file is ok
3.Especially, this is a MFC application. I highly recommend you to download the Visual Studio 2022 and install the necessary MFC tool or you will face the running problem. But this maybe the only problem and just enjoy the game!
download this project :(if helpful to you, why not star it ? haha)mu1guo/auto_mine_sweeper: A C++ project to design the best strategy for minesweeper game (github.com)