📄 ztyview.cpp
字号:
// ztyView.cpp : implementation of the CZtyView class
//
#include "stdafx.h"
#include "zty.h"
#include "ztyDoc.h"
#include "ztyView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CZtyView
IMPLEMENT_DYNCREATE(CZtyView, CFormView)
BEGIN_MESSAGE_MAP(CZtyView, CFormView)
//{{AFX_MSG_MAP(CZtyView)
ON_BN_CLICKED(IDC_PROMAP, OnPromap)
ON_BN_CLICKED(IDC_DESIGN, OnDesign)
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(IDC_BEGINGO, OnBegingo)
ON_BN_CLICKED(IDC_VIEWINFO, OnViewinfo)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CZtyView construction/destruction
CZtyView::CZtyView()
: CFormView(CZtyView::IDD)
{
//{{AFX_DATA_INIT(CZtyView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
N=200;
BtnState=0;
time=GetCurrentTime();
t=time.GetSecond();
}
CZtyView::~CZtyView()
{
}
void CZtyView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CZtyView)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BOOL CZtyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CZtyView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
((CEdit*)GetDlgItem(IDC_DESIGN))->EnableWindow(FALSE);
((CEdit*)GetDlgItem(IDC_BEGINGO))->EnableWindow(FALSE);
}
/////////////////////////////////////////////////////////////////////////////
// CZtyView printing
BOOL CZtyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CZtyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CZtyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CZtyView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CZtyView diagnostics
#ifdef _DEBUG
void CZtyView::AssertValid() const
{
CFormView::AssertValid();
}
void CZtyView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CZtyDoc* CZtyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CZtyDoc)));
return (CZtyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CZtyView message handlers
void CZtyView::OnPromap()
{
// TODO: Add your control notification handler code here
//初始化
BtnState=0;
maze.Init();
((CEdit*)GetDlgItem(IDC_DESIGN))->EnableWindow(TRUE);
((CEdit*)GetDlgItem(IDC_DESIGN))->SetWindowText("设定起点");
int num;
num=0;
do
{
int k=(rand()+t)%28;
int l=(rand()+3*t)%20;
if(maze.map[k][l]!=1)
{
maze.map[k][l]=1;
num++;
}
}while(num!=N);
InvalidateRect(&CRect(20,50,580,450));
}
void CZtyView::OnDraw(CDC* pDC)
{
// TODO: Add your specialized code here and/or call the base class
//画背景
CBrush mybrush1;
mybrush1.CreateSolidBrush(RGB(200,200,200));
CRect rect1;
GetClientRect(&rect1);
pDC->FillRect(&rect1,&mybrush1);
//画方框
CPen mypen;
CPen* oldopen;
mypen.CreatePen(PS_SOLID,1,RGB(0,0,0));
oldopen=pDC->SelectObject(&mypen);
pDC->MoveTo(20,50);
pDC->LineTo(20,450);
pDC->LineTo(580,450);
pDC->LineTo(580,50);
pDC->LineTo(20,50);
//画地图
CBrush mybrush2;
mybrush2.CreateSolidBrush(RGB(112,65,101));
CBrush mybrush3;
mybrush3.CreateSolidBrush(RGB(134,143,152));
CBrush mybrush4;
mybrush4.CreateSolidBrush(RGB(0,225,0));
CBrush mybrush5;
mybrush5.CreateSolidBrush(RGB(255,0,0));
CBrush mybrush9;
mybrush9.CreateSolidBrush(RGB(153,255,204));
for(int i=0;i<28;i++)
{
for(int j=0;j<20;j++)
{
CRect rect2(21+i*20,51+20*j,39+i*20,69+20*j);
switch(maze.map[i][j])
{
case 0: //画背景
pDC->FillRect(&rect2,&mybrush3);
break;
case 2: //画路径
pDC->FillRect(&rect2,&mybrush4);
break;
case 1: //画障碍
pDC->FillRect(&rect2,&mybrush2);
break;
case 3: //画小球
pDC->Ellipse(&rect2);
break;
case 4:
pDC->FillRect(&rect2,&mybrush5); //画终点
break;
case 9:
//pDC->DrawText("⊙",&rect2,DT_CENTER);
pDC->FillRect(&rect2,&mybrush9); //画路径
break;
}
}
}
}
void CZtyView::OnDesign()
{
// TODO: Add your control notification handler code here
switch(BtnState)
{
case 0:
MessageBox("请点击你欲设为迷宫起点的方格!");
BtnState=1;
break;
case 1:
MessageBox("请点击你欲设为迷宫终点的方格!");
BtnState=2;
break;
case 2:
MessageBox("你已经设定迷宫的终点和起点了!");
BtnState=3;
break;
}
}
void CZtyView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
int t=maze.Go();
int i,j,k,l;
// CRect rect1,rect2;
i=maze.prepos.x;
j=maze.prepos.y;
k=maze.curpos.x;
l=maze.curpos.y;
CRect rect1(21+i*20,51+20*j,39+i*20,69+20*j);
CRect rect2(21+k*20,51+20*l,39+k*20,69+20*l);
switch(t)
{
case 0:
KillTimer(1);
MessageBox("无路可走了!");
break;
case 1: //关键
maze.map[maze.prepos.x][maze.prepos.y]=2; //走过的点
maze.map[maze.curpos.x][maze.curpos.y]=3; //要到的点
InvalidateRect(&rect1);
InvalidateRect(&rect2);
break;
case 2:
KillTimer(1);
MessageBox("恭喜你,到达终点了!");
}
if(maze.curpos.x==maze.end.x&&maze.curpos.y==maze.end.y)
{
KillTimer(1);
MessageBox("恭喜你,到达终点了!");
}
CFormView::OnTimer(nIDEvent);
}
void CZtyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC *pDC=GetDC();
CDC Dc;
if(Dc.CreateCompatibleDC(pDC)==FALSE)
AfxMessageBox("Can't create DC");
int x,y;
x=point.x;
y=point.y;
int i,j;
i=(x-20)/20;
j=(y-50)/20;
CRect rect(21+i*20,51+20*j,39+i*20,69+20*j);
CString str;
switch(BtnState)
{
case 0:
case 3:
str.Format("鼠标的坐标为:%d,%d\r\n所在的数组下标为(%d,%d)",x,y,i,j);
MessageBox(str);
break;
case 1:
if(maze.map[i][j]==1)
MessageBox("此处为障碍,不能设为起点!");
else
{
maze.start.x=i; //定义起点
maze.start.y=j;
maze.curpos=maze.start; //设定当前位置
maze.map[i][j]=3; //画小球
InvalidateRect(&rect);
MessageBox("设定起点成功!");
BtnState=2;
((CEdit*)GetDlgItem(IDC_DESIGN))->SetWindowText("设定终点");
((CEdit*)GetDlgItem(IDC_DESIGN))->EnableWindow(FALSE);
UpdateWindow();
}
break;
case 2:
if(maze.map[i][j]==1)
MessageBox("此处为障碍,不能设为终点!");
else
{
maze.end.x=i;
maze.end.y=j;
maze.map[i][j]=4; //画终点
InvalidateRect(&rect);
MessageBox("设定终点成功!");
((CEdit*)GetDlgItem(IDC_BEGINGO))->EnableWindow(TRUE);
BtnState=3;
}
break;
}
CFormView::OnLButtonDown(nFlags, point);
}
void CZtyView::OnBegingo()
{
// TODO: Add your control notification handler code here
SetTimer(1,80,NULL);
}
void CZtyView::OnViewinfo()
{
// TODO: Add your control notification handler code here
CString str;
str.Format("起点坐标:(%d,%d)\r\n终点坐标:(%d,%d)",maze.start.x,maze.start.y,maze.end.x,maze.end.y);
MessageBox(str);
while(maze.top!=-1)
{
int i,j;
i=maze.s[maze.top].point.x;
j=maze.s[maze.top].point.y;
CRect rect(21+i*20,51+20*j,39+i*20,69+20*j);
maze.map[i][j]=9;
InvalidateRect(&rect);
--maze.top;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -