?? mfc挖雷view.cpp
字號:
// MFC挖雷View.cpp : implementation of the CMFCView class
//
#include "stdafx.h"
#include "MFC挖雷.h"
#include "MFC挖雷Doc.h"
#include "MFC挖雷View.h"
#include "SetSize.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int XDIVISIONS=9;
int YDIVISIONS=9;
int LEISHU=10;
/////////////////////////////////////////////////////////////////////////////
// CMFCView
IMPLEMENT_DYNCREATE(CMFCView, CView)
BEGIN_MESSAGE_MAP(CMFCView, CView)
//{{AFX_MSG_MAP(CMFCView)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_FILE_NEW, OnFileNew)
ON_COMMAND(ID_N1, OnN1)
ON_COMMAND(ID_N2, OnN2)
ON_COMMAND(ID_N3, OnN3)
ON_COMMAND(IDM_SET, OnSet)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_WM_TIMER()
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMFCView construction/destruction
int CMFCView::xw=15;
int CMFCView::yw=15;
int CMFCView::leiconunt=20;
int DCLCount=10;
int Ctime=0;
CMFCView::CMFCView():xAdd(25),yAdd(40),m_bStart(FALSE),FaceNum(4),m_MouseDown(FALSE)
{
// TODO: add construction code here
Back.LoadBitmap(IDB_BACK);
Back.GetObject(sizeof(BITMAP),&bk);
Face.LoadBitmap(IDB_FACE);
Face.GetObject(sizeof(BITMAP),&face);
NumberPic.LoadBitmap(IDB_NUMBER);
NumberPic.GetObject(sizeof(BITMAP),&Number);
}
CMFCView::~CMFCView()
{
}
BOOL CMFCView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMFCView drawing
void CMFCView::OnDraw(CDC* pDC)
{
DrawNumber(DCLCount,CPoint((faceRect.right-faceRect.left)/4,yAdd/4+1));
DrawFace(pDC,FaceNum,CPoint((faceRect.right-faceRect.left)/2,yAdd/4));
DrawNumber(Ctime,CPoint((faceRect.right-faceRect.left)/2+(faceRect.right-faceRect.left)/6,yAdd/4+1));
DrawBackGround();
CMFCDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CDC MemDC;
CBitmap *pOld;
MemDC.CreateCompatibleDC(pDC);
for (int i=0;i<XDIVISIONS;i++)
for (int j=0;j<YDIVISIONS;j++)
{
switch(map.Map(i,j))
{
case 0:
pOld=MemDC.SelectObject(&Back);
pDC->BitBlt(i*xw+xAdd,j*yw+yAdd,xw,yw,&MemDC,0,0,SRCCOPY);
MemDC.SelectObject(&pOld);
break;
case 1:
pOld=MemDC.SelectObject(&Back);
pDC->BitBlt(i*xw+xAdd,j*yw+yAdd,xw,yw,&MemDC,0,80,SRCCOPY);
MemDC.SelectObject(&pOld);
break;
case 2:
if (map.GetCount(i,j)==0)
{
pOld=MemDC.SelectObject(&Back);
pDC->BitBlt(i*xw+xAdd,j*yw+yAdd,xw,yw,&MemDC,0,237,SRCCOPY);
MemDC.SelectObject(&pOld);
}
else
{
pOld=MemDC.SelectObject(&Back);
pDC->BitBlt(i*xw+xAdd,j*yw+yAdd,xw,yw,&MemDC,0,237-map.GetCount(i,j)*16,SRCCOPY);
MemDC.SelectObject(&pOld);
}
break;
case 3:
pOld=MemDC.SelectObject(&Back);
pDC->BitBlt(i*xw+xAdd,j*yw+yAdd,xw,yw,&MemDC,0,16,SRCCOPY);
MemDC.SelectObject(&pOld);
break;
case 5:
pOld=MemDC.SelectObject(&Back);
pDC->BitBlt(i*xw+xAdd,j*yw+yAdd,xw,yw,&MemDC,0,46,SRCCOPY);
MemDC.SelectObject(&pOld);
break;
}
}
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMFCView diagnostics
#ifdef _DEBUG
void CMFCView::AssertValid() const
{
CView::AssertValid();
}
void CMFCView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMFCDoc* CMFCView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCDoc)));
return (CMFCDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMFCView message handlers
void CMFCView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC=GetDC();
if (point.x>=facp.x && point.x<=facp.x+24 &&
point.y>=facp.y && point.y<=facp.y+24)
{
SetNewGames();
FaceNum=0;
DrawFace(pDC,FaceNum,CPoint((faceRect.right-faceRect.left)/2,yAdd/4));
ReleaseDC(pDC);
return;
}
DrawFace(pDC,3,CPoint((faceRect.right-faceRect.left)/2,yAdd/4));
ReleaseDC(pDC);
if (m_bStart==FALSE) return;
if (m_MouseDown==FALSE)
m_MouseDown=TRUE;
int x,y;
x=(point.x-xAdd)/xw;
if (x>=XDIVISIONS)
{
CView::OnLButtonDown(nFlags, point);
return;
}
y=(point.y-yAdd)/yw;
if (y>=YDIVISIONS)
{
CView::OnLButtonDown(nFlags, point);
return;
}
if (map.GetMap(x,y)==3)
{
}
else if (map.GetLei(x,y)==1)
{
for (int i=0;i<XDIVISIONS;i++)
for (int j=0;j<YDIVISIONS;j++)
if (map.GetLei(i,j)==1)
{
map.SetMap(i,j,1);
map.SetEnd(1);
}
FaceNum=2;
map.SetMap(x,y,5);
Invalidate();
::MessageBox(NULL,"挖雷失敗","挖雷失敗",MB_OK);
m_MouseDown=m_bStart=false;
}
else
{
map.SetMap(x,y,2);
}
CView::OnLButtonDown(nFlags, point);
}
void CMFCView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_bStart==FALSE) return;
int x,y;
if (m_MouseDown==FALSE)
m_MouseDown=TRUE;
x=(point.x-xAdd)/xw;
if (x>=XDIVISIONS) {
CView::OnRButtonDown(nFlags, point);
return;
}
y=(point.y-yAdd)/yw;
if (y>=YDIVISIONS) {
CView::OnRButtonDown(nFlags, point);
return;
}
if (map.GetMap(x,y)==3)
{
map.SetMap(x,y,0);
DCLCount++;
}
else if (map.GetMap(x,y)!=2)
{
map.SetMap(x,y,3);
DCLCount--;
}
Invalidate();
CView::OnRButtonDown(nFlags, point);
}
void CMFCView::SetNewGames()
{
KillTimer(0);
SetTimer(0,1000,NULL);
map.InitMap();
Invalidate();
DCLCount=LEISHU;
m_MouseDown=FALSE;
Ctime=0;
FaceNum=4;
m_bStart=TRUE;
AfxGetMainWnd()->SetWindowPos( NULL, 0, 0, 60+XDIVISIONS*xw,
110+YDIVISIONS*yw, SWP_NOMOVE | SWP_NOZORDER );
GetClientRect(&faceRect);
}
void CMFCView::OnFileNew()
{
// TODO: Add your command handler code here
SetNewGames();
}
void CMFCView::OnN1()
{
// TODO: Add your command handler code here
XDIVISIONS=YDIVISIONS=9;
LEISHU=10;
SetNewGames();
}
void CMFCView::OnN2()
{
// TODO: Add your command handler code here
XDIVISIONS=YDIVISIONS=16;
LEISHU=40;
SetNewGames();
}
void CMFCView::OnN3()
{
// TODO: Add your command handler code here
XDIVISIONS=30;
YDIVISIONS=16;
LEISHU=99;
SetNewGames();
}
void CMFCView::OnSet()
{
// TODO: Add your command handler code here
CSetSize set;
set.m_col=XDIVISIONS;
set.m_row=YDIVISIONS;
set.m_count=LEISHU;
if (set.DoModal()==IDOK)
{
XDIVISIONS=set.m_col;
YDIVISIONS=set.m_row;
LEISHU=set.m_count;
SetNewGames();
}
GetClientRect(&faceRect);
}
void CMFCView::OnButton1()
{
// TODO: Add your control notification handler code here
OnFileNew();
}
void CMFCView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (m_MouseDown)
{
Ctime++;
DrawNumber(Ctime,CPoint((faceRect.right-faceRect.left)/2+(faceRect.right-faceRect.left)/6,yAdd/4+1));
}
CView::OnTimer(nIDEvent);
}
int CMFCView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SetTimer(0,1000,NULL);
m_bStart=TRUE;
return 0;
}
void CMFCView::OnDestroy()
{
CView::OnDestroy();
KillTimer(0);
// TODO: Add your message handler code here
}
void CMFCView::OnSize(UINT nType, int cx, int cy)
{
GetClientRect(&faceRect);
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
}
void CMFCView::DrawFace(CDC* pDC,int FaceNumber,CPoint pt)
{
facp=pt;
CDC MemDC;
CBitmap *pOld;
MemDC.CreateCompatibleDC(pDC);
pOld=MemDC.SelectObject(&Face);
pDC->BitBlt(pt.x,pt.y,24,24,&MemDC,0,FaceNumber*24,SRCCOPY);
MemDC.SelectObject(&pOld);
}
void CMFCView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC=GetDC();
if (point.x>=facp.x && point.x<=facp.x+24 &&
point.y>=facp.y && point.y<=facp.y+24)
{
SetNewGames();
FaceNum=4;
DrawFace(pDC,FaceNum,CPoint((faceRect.right-faceRect.left)/2,yAdd/4));
ReleaseDC(pDC);
return;
}
bool m_ok=true;
for (int i=0;i<XDIVISIONS;i++)
for (int j=0;j<YDIVISIONS;j++)
if (map.GetMap(i,j)!=2 && map.GetLei(i,j)!=1)
m_ok=false;
if (m_ok==true && m_bStart==TRUE)
{
map.SetEnd(1);
Invalidate();
::MessageBox(NULL,"挖雷成功",("挖雷成功"),MB_OK);
m_MouseDown=false;
m_bStart=FALSE;
}
DrawFace(pDC,FaceNum,CPoint((faceRect.right-faceRect.left)/2,yAdd/4));
ReleaseDC(pDC);
Invalidate();
CView::OnLButtonUp(nFlags, point);
}
void CMFCView::DrawNumber(int number, CPoint pt)
{
int a[3];
bool f=false;
if (number<0)
{
f=true;
number=-number;
}
a[0]=number/100;
a[1]=(number-a[0]*100)/10;
a[2]=number%10;
CDC* pDC=GetDC();
CDC MemDC;
CBitmap *pOld;
MemDC.CreateCompatibleDC(pDC);
pOld=MemDC.SelectObject(&NumberPic);
for (int i=0;i<3;i++)
{
if (f && i==0)
{
pDC->BitBlt(pt.x+i*12,pt.y,12,22,&MemDC,0,0,SRCCOPY);
continue;
}
switch(a[i])
{
case 0:
pDC->BitBlt(pt.x+i*12,pt.y,12,22,&MemDC,0,253,SRCCOPY);
break;
case 1:
pDC->BitBlt(pt.x+i*12,pt.y,12,22,&MemDC,0,231,SRCCOPY);
break;
case 2:
pDC->BitBlt(pt.x+i*12,pt.y,12,22,&MemDC,0,207,SRCCOPY);
break;
case 3:
pDC->BitBlt(pt.x+i*12,pt.y,12,22,&MemDC,0,184,SRCCOPY);
break;
case 4:
pDC->BitBlt(pt.x+i*12,pt.y,12,22,&MemDC,0,162,SRCCOPY);
break;
case 5:
pDC->BitBlt(pt.x+i*12,pt.y,12,22,&MemDC,0,138,SRCCOPY);
break;
case 6:
pDC->BitBlt(pt.x+i*12,pt.y,12,22,&MemDC,0,115,SRCCOPY);
break;
case 7:
pDC->BitBlt(pt.x+i*12,pt.y,12,22,&MemDC,0,92,SRCCOPY);
break;
case 8:
pDC->BitBlt(pt.x+i*12,pt.y,12,22,&MemDC,0,69,SRCCOPY);
break;
case 9:
pDC->BitBlt(pt.x+i*12,pt.y,12,22,&MemDC,0,46,SRCCOPY);
break;
}
}
MemDC.SelectObject(&pOld);
ReleaseDC(pDC);
}
void CMFCView::DrawBackGround()
{
CDC* pDC=GetDC();
pDC->MoveTo(3,3);//192,192,192
CBrush GBursh(RGB(192,192,192));
//top
CRect rect(7,2,faceRect.right,7);
pDC->FillRect(&rect,&GBursh);
//left
rect.bottom=faceRect.bottom;
rect.right=12;
pDC->FillRect(&rect,&GBursh);
//bottom
rect.top=faceRect.bottom-7;
rect.bottom=faceRect.bottom;
rect.right=faceRect.right;
pDC->FillRect(&rect,&GBursh);
//right
rect.top=2;
rect.left=faceRect.right-7;
pDC->FillRect(&rect,&GBursh);
CBrush HBursh(RGB(128,128,128));
//top
CRect rect2(12,7,faceRect.right-7,8);
pDC->FillRect(&rect2,&HBursh);
//left
rect.right=13;
rect.bottom=faceRect.bottom;
pDC->FillRect(&rect2,&HBursh);
ReleaseDC(pDC);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -