📄 ptgamedlg.cpp
字号:
// PtGameDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PtGame.h"
#include "PtGameDlg.h"
#include "SettingDlg.h"
#include "LookDlg.h"
#include <mmsystem.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()
/////////////////////////////////////////////////////////////////////////////
// CPtGameDlg dialog
CPtGameDlg::CPtGameDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPtGameDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPtGameDlg)
// 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);
hnums=AfxGetApp()->GetProfileInt(_T("游戏参数"),_T("hnums"),4);
vnums=AfxGetApp()->GetProfileInt(_T("游戏参数"),_T("vnums"),3);
gamepicture=AfxGetApp()->GetProfileInt(_T("游戏参数"),_T("gamepicture"),IDB_BITMAP3);
musiconoff=AfxGetApp()->GetProfileInt(_T("游戏参数"),_T("musiconoff"),FALSE);
musicfilename=AfxGetApp()->GetProfileString(_T("游戏参数"),_T("musicfilename"),"");
blacksite.x =hnums;
blacksite.y =vnums;
tracksteps=0;
tempsteps=0;
autoplay=FALSE;
m_bMouseTell=FALSE;
m_iStart=0;
m_strInfo=" Please Write To Me. E-Mail: qsxsxh@163.com. Thank You!! 2002-08-20 陈伟峰 ";
}
CPtGameDlg::~CPtGameDlg()
{
DeleteObject(bitmap);
AfxGetApp()->WriteProfileInt(_T("游戏参数"),_T("hnums"),hnums);
AfxGetApp()->WriteProfileInt(_T("游戏参数"),_T("vnums"),vnums);
AfxGetApp()->WriteProfileInt(_T("游戏参数"),_T("gamepicture"),gamepicture);
AfxGetApp()->WriteProfileInt(_T("游戏参数"),_T("musiconoff"),musiconoff);
AfxGetApp()->WriteProfileString(_T("游戏参数"),_T("musicfilename"),musicfilename);
}
void CPtGameDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPtGameDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPtGameDlg, CDialog)
//{{AFX_MSG_MAP(CPtGameDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_KEYDOWN()
ON_WM_LBUTTONDOWN()
ON_COMMAND(IDM_NEWGAME, OnNewgame)
ON_COMMAND(IDM_AUTOPLAY, OnAutoplay)
ON_WM_TIMER()
ON_COMMAND(IDM_EXIT, OnExit)
ON_COMMAND(IDM_SET, OnSet)
ON_COMMAND(IDM_USERPIC, OnUserpic)
ON_COMMAND(IDM_ABOUT, OnAbout)
ON_COMMAND(IDM_PRELOOK, OnPrelook)
ON_COMMAND(IDM_PIC1, OnPic1)
ON_COMMAND(IDM_PIC2, OnPic2)
ON_COMMAND(IDM_PIC3, OnPic3)
ON_COMMAND(IDM_PIC4, OnPic4)
ON_COMMAND(IDM_PIC5, OnPic5)
ON_COMMAND(IDM_PIC6, OnPic6)
ON_WM_CLOSE()
ON_COMMAND(IDM_MOUSETELL, OnMousetell)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPtGameDlg message handlers
BOOL CPtGameDlg::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
//创建位图菜单
HMENU hmenu;
hmenu=::GetMenu(m_hWnd);
hmenu=::GetSubMenu(hmenu,1);
char * hbmp;
hbmp=(char *)LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_PIC1));
::ModifyMenu(hmenu,2,MF_BYPOSITION|MF_BITMAP,::GetMenuItemID(hmenu,2),hbmp);
hbmp=(char *)LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_PIC2));
::ModifyMenu(hmenu,3,MF_BYPOSITION|MF_BITMAP,::GetMenuItemID(hmenu,3),hbmp);
hbmp=(char *)LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_PIC3));
::ModifyMenu(hmenu,4,MF_BYPOSITION|MF_BITMAP,::GetMenuItemID(hmenu,4),hbmp);
hbmp=(char *)LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_PIC4));
::ModifyMenu(hmenu,5,MF_BYPOSITION|MF_BITMAP,::GetMenuItemID(hmenu,5),hbmp);
hbmp=(char *)LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_PIC5));
::ModifyMenu(hmenu,6,MF_BYPOSITION|MF_BITMAP,::GetMenuItemID(hmenu,6),hbmp);
hbmp=(char *)LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_PIC6));
::ModifyMenu(hmenu,7,MF_BYPOSITION|MF_BITMAP,::GetMenuItemID(hmenu,7),hbmp);
//创建状态栏
m_wndStatusBar.Create(WS_CHILD|WS_VISIBLE|CCS_BOTTOM,CRect(0,0,0,0),this,102);
int iPartDim[3]={100,370,-1};
m_wndStatusBar.SetParts(3,iPartDim);
m_wndStatusBar.SetText("正在游戏",0,0);
m_wndStatusBar.SetText("用方向键移动,或用鼠标左键单击黑块周围图块",1,0);
CRect rc;
GetClientRect(&rc);
clientwidth=rc.Width();
clientheight=rc.Height()-20;
cellwidth=clientwidth/hnums;
cellheight=clientheight/vnums;
bitmap.LoadBitmap(gamepicture);
initialsiteinfo();
SetTimer(2,150,NULL);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CPtGameDlg::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 CPtGameDlg::OnPaint()
{
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
{
int i;
int j;
struct site destsite;
struct site srcsite;
for(i = 1;i<=hnums;i++)
{
for(j = 1;j<=vnums;j++)
{
srcsite.x = siteinfo[i][j].x;
srcsite.y = siteinfo[i][j].y;
destsite.x = i;
destsite.y = j;
paintpic(destsite, srcsite);
}
}
paintline();
paintblack();
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPtGameDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPtGameDlg::initialsiteinfo()
{
int i,j;
for(i=1;i<=hnums;i++)
{
for(j=1;j<=vnums;j++)
{
siteinfo[i][j].x =i;
siteinfo[i][j].y =j;
}
}
return;
}
void CPtGameDlg::paintline()
{
int i;
CDC *pDC=GetDC();
for(i=1;i<hnums;i++)
{
pDC->MoveTo(i*cellwidth-1,0);
pDC->LineTo(i*cellwidth-1,clientheight);
}
for(i=1;i<vnums;i++)
{
pDC->MoveTo(0,i*cellheight-1);
pDC->LineTo(clientwidth,i*cellheight-1);
}
pDC->DeleteDC();
return;
}
void CPtGameDlg::paintblack()
{
RECT rc;
rc.left =(blacksite.x-1)*cellwidth;
rc.top =(blacksite.y -1)*cellheight;
rc.right=rc.left +cellwidth;
rc.bottom =rc.top +cellheight;
CDC *pDC=GetDC();
pDC->FillSolidRect(&rc,RGB(0,0,0));
pDC->DeleteDC();
return;
}
void CPtGameDlg::paintpic(struct site destsite, struct site srcsite)
{
CDC *pDC=GetDC();
CDC memdc;
memdc.CreateCompatibleDC(pDC);
memdc.SelectObject(bitmap);
pDC->BitBlt((destsite.x-1)*cellwidth,(destsite.y-1)*cellheight,cellwidth,cellheight,&memdc,(srcsite.x-1)*cellwidth,(srcsite.y-1)*cellheight,SRCCOPY);
pDC->DeleteDC();
return;
}
BOOL CPtGameDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_KEYDOWN)
{
OnKeyDown((UINT)pMsg->wParam,(UINT)pMsg->lParam,(UINT)pMsg->lParam);
}
return CDialog::PreTranslateMessage(pMsg);
}
void CPtGameDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
struct site tempsite;
switch(nChar)
{
case VK_LEFT:
if(blacksite.x>1)
{
tracksteps+=1;
if(tracksteps>1000)
{
MessageBox("所定义数组空间已用完,你水平太臭了!","俄罗斯方块游戏",MB_OK|MB_ICONINFORMATION);
CDialog::EndDialog(0);
}
track[tracksteps]=1;
tempsite.x =blacksite.x-1;
tempsite.y =blacksite.y ;
siteinfo[blacksite.x][blacksite.y].x = siteinfo[tempsite.x][tempsite.y].x;
siteinfo[blacksite.x][blacksite.y].y = siteinfo[tempsite.x][tempsite.y].y;
tempsite.x = siteinfo[blacksite.x][blacksite.y].x;
tempsite.y = siteinfo[blacksite.x][blacksite.y].y;
paintpic(blacksite,tempsite);
blacksite.x = blacksite.x - 1;
paintblack();
paintline();
if(tracksteps > 1)
{
if(track[tracksteps - 1]== 2)
{
tracksteps = tracksteps - 2;
}
}
}
break;
case VK_RIGHT:
if(blacksite.x<hnums)
{
tracksteps+=1;
if(tracksteps>1000)
{
MessageBox("所定义数组空间已用完,你水平太臭了!","俄罗斯方块游戏",MB_OK|MB_ICONINFORMATION);
CDialog::EndDialog(0);
}
track[tracksteps]=2;
tempsite.x =blacksite.x+1;
tempsite.y =blacksite.y ;
siteinfo[blacksite.x][blacksite.y].x = siteinfo[tempsite.x][tempsite.y].x;
siteinfo[blacksite.x][blacksite.y].y = siteinfo[tempsite.x][tempsite.y].y;
tempsite.x = siteinfo[blacksite.x][blacksite.y].x;
tempsite.y = siteinfo[blacksite.x][blacksite.y].y;
paintpic(blacksite,tempsite);
blacksite.x = blacksite.x + 1;
paintblack();
paintline();
if(tracksteps > 1)
{
if(track[tracksteps - 1]== 1)
{
tracksteps = tracksteps - 2;
}
}
}
break;
case VK_UP:
if(blacksite.y>1)
{
tracksteps+=1;
if(tracksteps>1000)
{
MessageBox("所定义数组空间已用完,你水平太臭了!","俄罗斯方块游戏",MB_OK|MB_ICONINFORMATION);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -