📄 snakegamedlg.cpp
字号:
// SnakeGameDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SnakeGame.h"
#include "SnakeGameDlg.h"
#include "SkinMagicLib.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
const int BOARD_TOP = 15;
const int BOARD_LEFT = 12;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CSnakeGameDlg dialog
CSnakeGameDlg::CSnakeGameDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSnakeGameDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSnakeGameDlg)
// 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 CSnakeGameDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSnakeGameDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSnakeGameDlg, CDialog)
//{{AFX_MSG_MAP(CSnakeGameDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSnakeGameDlg message handlers
BOOL CSnakeGameDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
//程序只启动一次时做标志
//bool b = ::SetProp(this->m_hWnd, AfxGetApp()->m_pszExeName, (HANDLE)6);
const CWnd * pWndInsertAfter;
pWndInsertAfter = &wndTopMost;
SetWindowPos(pWndInsertAfter,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE);
// 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
CRect rtBoard(BOARD_LEFT, BOARD_TOP,
BOARD_LEFT + (COLS + COL_CTL) * WIDTH + 3,
BOARD_TOP + ROWS * WIDTH);
m_BoardOne.Create(NULL, NULL, WS_VISIBLE, rtBoard, this,
GAME_BOARD_ONE, NULL);
//在这里初始化SkinMagicLib
VERIFY( 1 == InitSkinMagicLib( AfxGetInstanceHandle(), "SnakeGame" ,
NULL,
NULL ) );
VERIFY( 1 == LoadSkinFromResource( AfxGetInstanceHandle(), "IDR_XP", "SKIN"));
SetWindowSkin( m_hWnd , NULL );
VERIFY( 1 == SetDialogSkin( "Dialog" ) );
return TRUE; // return TRUE unless you set the focus to a control
}
void CSnakeGameDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
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 CSnakeGameDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
if (IsIconic())
{
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();
// Draw Frame border
CRect rt;
GetDlgItem(GAME_BOARD_ONE)->GetWindowRect(&rt);
ScreenToClient(&rt);
rt.InflateRect(3,3);
DrawBoardBorder((CDC*)&dc, rt.TopLeft(), rt.Width(),
rt.Height(), 1, RGB(255,0,0));
rt.DeflateRect(1,1);
DrawBoardBorder((CDC*)&dc, rt.TopLeft(), rt.Width(),
rt.Height(), 1, RGB(0,0,255));
rt.DeflateRect(1,1);
DrawBoardBorder((CDC*)&dc, rt.TopLeft(), rt.Width(),
rt.Height(), 1, RGB(0,255,0));
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSnakeGameDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CSnakeGameDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message == WM_KEYDOWN)
{
switch (pMsg->wParam)
{
case VK_DOWN:
if (m_BoardOne.GetGameState() == CBoard::GAME_PLAYING)
m_BoardOne.Move(CBoard::MOVE_DOWN);
break;
case VK_UP:
if (m_BoardOne.GetGameState() == CBoard::GAME_PLAYING)
m_BoardOne.Move(CBoard::MOVE_UP);
break;
case VK_LEFT:
if (m_BoardOne.GetGameState() == CBoard::GAME_PLAYING)
m_BoardOne.Move(CBoard::MOVE_LEFT);
break;
case VK_RIGHT:
if (m_BoardOne.GetGameState() == CBoard::GAME_PLAYING)
m_BoardOne.Move(CBoard::MOVE_RIGHT);
break;
case VK_SPACE:
if (m_BoardOne.GetGameState() == CBoard::GAME_READY)
m_BoardOne.StartGame();
break;
case VK_RETURN:
break;
case 'A':
if (m_BoardOne.GetGameState() == CBoard::GAME_PLAYING)
m_BoardOne.SetSpeed(1, false);
break;
case 'P':
if (m_BoardOne.GetGameState() != CBoard::GAME_READY)
m_BoardOne.PauseGame();
break;
case 'R':
if (m_BoardOne.GetGameState() != CBoard::GAME_READY)
m_BoardOne.ResumeGame();
break;
default:
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
// Draw a rectangle border
void CSnakeGameDlg::DrawBoardBorder(CDC *pDC, CPoint ptStart, int nWidth,
int nHeight, int nPenWidth, COLORREF cr)
{
CPen pen( PS_SOLID, nPenWidth, cr );
CPen *pOldPen = pDC->SelectObject(&pen);
pDC->MoveTo( ptStart.x, ptStart.y );
pDC->LineTo( ptStart.x + nWidth, ptStart.y );
pDC->LineTo( ptStart.x + nWidth, ptStart.y + nHeight );
pDC->LineTo( ptStart.x, ptStart.y + nHeight );
pDC->LineTo( ptStart.x, ptStart.y );
pDC->SelectObject( pOldPen );
pen.DeleteObject();
}
void CSnakeGameDlg::OnOK()
{
}
void CSnakeGameDlg::OnCancel()
{
if (MessageBox("您真得要退出吗?","提示", MB_OKCANCEL | MB_ICONQUESTION) == IDOK)
CDialog::OnCancel();
}
void CSnakeGameDlg::OnDestroy()
{
CDialog::OnDestroy();
ExitSkinMagicLib();
// TODO: Add your message handler code here
}
BOOL CSnakeGameDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
//移除只运行一次标志
//HANDLE h = ::RemoveProp(this->m_hWnd, AfxGetApp()->m_pszExeName);
return CDialog::DestroyWindow();
}
void CSnakeGameDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
CDialog::OnClose();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -