📄 例子view.cpp
字号:
void CMyView::OnFileSave()
{
// TODO: Add your command handler code here
//设置保存的文件,后缀名maz
CFileDialog dlg(FALSE,"maz",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"迷宫地图(*.MAZ)|*.maz|All Files|*.*||",this);
if(dlg.DoModal()==IDOK)
dlg.GetFileName();
else
return;
CString str;
int i,j;
CStdioFile file;
if(file.Open(dlg.GetFileName(),CFile::modeCreate|CFile::modeWrite|CFile::typeText)==0)
{
AfxMessageBox("save error!");
return;
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(arry[i][j]==0)//如果有墙
file.WriteString("0\n");
else if(arry[i][j]==1)//
file.WriteString("1\n");
else if(arry[i][j]==3)//终点
file.WriteString("3\n");
else
file.WriteString("-1\n");
}
file.Close();
}
void CMyView::OnMapNew()
{
// TODO: Add your command handler code here
//只画外墙
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(i==0||i==m-1||j==0||j==n-1)
arry[i][j]=0;//外墙
else arry[i][j]=2;
}
xnow=1;
ynow=1;
x_start=xnow;
y_start=ynow;
arry[1][1]=1;//出生位置
xend=m-2;
yend=n-2;
arry[xend][yend]=3;//终点位置
Invalidate(false);//更新,不用OnPaint()
MessageBox("此功能将帮助你新建一张地图!\n\n\t记得保存!");
}
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC;
pDC=GetDC();
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
int x=((point.x-x_start)/15);
int y=((point.y-y_start)/15);
//判断是否在迷宫内
if(x<=0||x>=m-1||y<=0||y>=n-1)
MessageBox("请在迷宫内编辑!");
else
{
//根据鼠标形状画图
if(cur_flag==0)
{//将当前状态设为墙
if(arry[x][y]==1||arry[x][y]==3)//是否在起点或终点上
MessageBox("墙不能画在起点或终点上!");
else
{
MemDC.SelectObject(m_bitmap[0]);
pDC->BitBlt(x_start+x*15,y_start+y*15,15,15,&MemDC,0,0,SRCCOPY);
arry[x][y]=0;
}
}
if(cur_flag==1)
{//将当前状态设为起点
if(arry[x][y]==3)//鼠标所制位置是否为终点
MessageBox("起点和终点不能在同一位置!");
else
{///////////////////////////先擦除,后重画
MemDC.SelectObject(m_bitmap[2]);//设为黑色
pDC->BitBlt(x_start+xnow*15,y_start+ynow*15,15,15,&MemDC,0,0,SRCCOPY);
arry[xnow][ynow]=2;//擦除原来的起点
MemDC.SelectObject(m_bitmap[1]);
pDC->BitBlt(x_start+x*15,y_start+y*15,15,15,&MemDC,0,0,SRCCOPY);
arry[x][y]=1;
xnow=x;//设当前点为起点
ynow=y;
xstart=xnow;//保存当前点
ystart=ynow;
}
}
if(cur_flag==2)
{//将当前的墙擦除
if(arry[x][y]==1)
MessageBox("不能没有起点!");
else if(arry[x][y]==3)
MessageBox("不能没有终点!");
else
{
MemDC.SelectObject(m_bitmap[2]);
pDC->BitBlt(x_start+x*15,y_start+y*15,15,15,&MemDC,0,0,SRCCOPY);
arry[x][y]=2;//其他状态
}
}
if(cur_flag==3)
{//将当前状态设为终点
if(arry[x][y]==1)//鼠标所制位置是否为起点
MessageBox("终点和起点不能在同一位置!");
else
{
MemDC.SelectObject(m_bitmap[2]);//设为黑色
pDC->BitBlt(x_start+xend*15,y_start+yend*15,15,15,&MemDC,0,0,SRCCOPY);
arry[xend][yend]=2;//擦除原来的起点
MemDC.SelectObject(m_bitmap[3]);
pDC->BitBlt(x_start+x*15,y_start+y*15,15,15,&MemDC,0,0,SRCCOPY);
arry[x][y]=3;
xend=x;//设当前点为终点
yend=y;
}
}
}
CView::OnLButtonDown(nFlags, point);
}
BOOL CMyView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
if(nHitTest==HTCLIENT)
{
if(cur_flag==-1)
return CView::OnSetCursor(pWnd, nHitTest, message);
else
{
if(cur_flag==0)
::SetCursor(hcursor[0]);
if(cur_flag==1)
SetCursor(hcursor[1]);
if(cur_flag==2)
SetCursor(hcursor[2]);
if(cur_flag==3)
SetCursor(hcursor[3]);
return 1;
}
}
return CView::OnSetCursor(pWnd, nHitTest, message);
}
////////////////////////////////////////////////////////////////////////////
///以下是菜单项的代码
////////////////////////////////////////////////////////////////////////////
void CMyView::OnMENUstart()
{
// TODO: Add your command handler code here
cur_flag=1;
}
void CMyView::OnMENUend()
{
// TODO: Add your command handler code here
cur_flag=3; //qi
}
void CMyView::OnMENUcachu()
{
// TODO: Add your command handler code here
cur_flag=2; //black
}
void CMyView::OnMENUqiang()
{
// TODO: Add your command handler code here
cur_flag=0;
}
void CMyView::OnMENUcancel()
{
// TODO: Add your command handler code here
cur_flag=-1;
}
void CMyView::OnMENUplay()//回朔法自动演示
{// TODO: Add your command handler code here
CDC* pDCplay;
CDC MemDCplay;
pDCplay=GetDC();
MemDCplay.CreateCompatibleDC(pDCplay);
int i=4;//堆栈数组的下标
struct pt{
int mp;
int np;
}point[1200];
point[i].mp=xnow;
point[i].np=ynow;
arry[xnow][ynow]=i+1;//5开始表示走过
int time=m_time;
do{
if(arry[xnow][++ynow]==2||arry[xnow][ynow]==3)//如果向东可走y++
{
i++;
point[i].mp=xnow;
point[i].np=ynow;
arry[xnow][ynow]=i+1;//表示走过,下标从4开始
MemDCplay.SelectObject(m_bitmap[2]);
pDCplay->BitBlt(x_start+xnow*15,y_start+(ynow-1)*15,15,15,&MemDCplay,0,0,SRCCOPY);
MemDCplay.SelectObject(m_bitmap[1]);
pDCplay->BitBlt(x_start+xnow*15,y_start+ynow*15,15,15,&MemDCplay,0,0,SRCCOPY);
Sleep(time);
}else//判断 南西北
{--ynow;
if(arry[++xnow][ynow]==2||arry[xnow][ynow]==3)//如果向南可走x++
{
i++;
point[i].mp=xnow;
point[i].np=ynow;
arry[xnow][ynow]=i+1;//表示走过
MemDCplay.SelectObject(m_bitmap[2]);
pDCplay->BitBlt(x_start+(xnow-1)*15,y_start+ynow*15,15,15,&MemDCplay,0,0,SRCCOPY);
MemDCplay.SelectObject(m_bitmap[1]);
pDCplay->BitBlt(x_start+xnow*15,y_start+ynow*15,15,15,&MemDCplay,0,0,SRCCOPY);
Sleep(time);
}else//判断 西北
{--xnow;
if(arry[xnow][--ynow]==2||arry[xnow][ynow]==3)//如果向西可走
{
i++;
point[i].mp=xnow;
point[i].np=ynow;
arry[xnow][ynow]=i+1;//表示走过
MemDCplay.SelectObject(m_bitmap[2]);
pDCplay->BitBlt(x_start+xnow*15,y_start+(ynow+1)*15,15,15,&MemDCplay,0,0,SRCCOPY);
MemDCplay.SelectObject(m_bitmap[1]);
pDCplay->BitBlt(x_start+xnow*15,y_start+ynow*15,15,15,&MemDCplay,0,0,SRCCOPY);
Sleep(time);
}else//判断 北
{++ynow;
if(arry[--xnow][ynow]==2||arry[xnow][ynow]==3)//如果向北可走
{
i++;
point[i].mp=xnow;
point[i].np=ynow;
arry[xnow][ynow]=i+1;//表示走过
MemDCplay.SelectObject(m_bitmap[2]);
pDCplay->BitBlt(x_start+(xnow+1)*15,y_start+ynow*15,15,15,&MemDCplay,0,0,SRCCOPY);
MemDCplay.SelectObject(m_bitmap[1]);
pDCplay->BitBlt(x_start+xnow*15,y_start+ynow*15,15,15,&MemDCplay,0,0,SRCCOPY);
Sleep(time);
}else//四个方向都不能走,则退一步(回朔)
{++xnow;
arry[xnow][ynow]=-1;//将上一步设为-1
MemDCplay.SelectObject(m_bitmap[2]);
pDCplay->BitBlt(x_start+xnow*15,y_start+ynow*15,15,15,&MemDCplay,0,0,SRCCOPY);
i--;//退一步
if(i==3)
{
break;
}
xnow=point[i].mp;//
ynow=point[i].np;
MemDCplay.SelectObject(m_bitmap[1]);
pDCplay->BitBlt(x_start+xnow*15,y_start+ynow*15,15,15,&MemDCplay,0,0,SRCCOPY);
Sleep(time);
}
}
}
}
}
while(!(xnow==xend&&ynow==yend));
if(xnow==xend&&ynow==yend||i==3)//成功结束
{
if(xnow==xend&&ynow==yend)
MessageBox("You win!!");
if(i==3)
MessageBox("No win!!");
xnow=xstart;//保存起点
ynow=ystart;
for(int i1=0;i1<m;i1++)
for(int j=0;j<n;j++)
if(arry[i1][j]!=0)
{
if(xnow==i1&&ynow==j)
arry[i1][j]=1;
else if(xend==i1&¥d==j)
arry[i1][j]=3;
else arry[i1][j]=2;
}
Invalidate();
}
}
void CMyView::OnMENUstop()
{
// TODO: Add your command handler code here
if(stop==false)
stop=true;
else
stop=false;
}
/////////////////////////////////////////////////////////////////////
///以下是设定时间间隔的代码
//////////////////////////////////////////////////////////////////////
void CMyView::Ontime1000()
{
// TODO: Add your command handler code here
m_time=1000;
}
void CMyView::OnUpdatetime1000(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_time==1000);
}
void CMyView::Ontime700()
{
// TODO: Add your command handler code here
m_time=700;
}
void CMyView::OnUpdatetime700(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_time==700);
}
void CMyView::Ontime400()
{
// TODO: Add your command handler code here
m_time=400;
}
void CMyView::OnUpdatetime400(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_time==400);
}
void CMyView::Ontime200()
{
// TODO: Add your command handler code here
m_time=200;
}
void CMyView::OnUpdatetime200(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_time==200);
}
void CMyView::Ontime100()
{
// TODO: Add your command handler code here
m_time=100;
}
void CMyView::OnUpdatetime100(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_time==100);
}
void CMyView::Ontime50()
{
// TODO: Add your command handler code here
m_time=50;
}
void CMyView::OnUpdatetime50(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_time==50);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -