📄 mfctttview.cpp
字号:
// mfctttView.cpp
//
#include "stdafx.h"
#include "mfcttt.h"
#include "mfctttDoc.h"
#include "mfctttView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////
// CMfctttView
IMPLEMENT_DYNCREATE(CMfctttView, CView)
BEGIN_MESSAGE_MAP(CMfctttView, CView)
//{{AFX_MSG_MAP(CMfctttView)
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
////////////////////////////////
// CMfctttView construction/destruction
CMfctttView::CMfctttView()
{
// TODO: add construction code here
}
CMfctttView::~CMfctttView()
{
}
BOOL CMfctttView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
////////////////////////////////
// CMfctttView drawing
// Offset from edge of tic tac toe cell
#define OFFSET 15
void CMfctttView::OnDraw(CDC* pDC)
{
CMfctttDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// Create pens
// grid pen
CPen p(PS_SOLID,10,RGB(0,0,0));
// X pen
CPen xp(PS_SOLID,5,RGB(0xFF,0,0));
// O pen
CPen op(PS_SOLID,5,RGB(0,0,0xFF));
CPen *old;
// draw board
CRect r;
GetClientRect(&r);
old=pDC->SelectObject(&p);
// Here is some debugging code
// to try fonts
//CFont f,*oldf;
//f.CreateFont(-30,0,0,0,0,0,0,0,0,0,0,0,0,"Arial" );
//oldf=pDC->SelectObject(&f);
//pDC->TextOut(0,0,"Hello");
//pDC->SelectObject(oldf);
// Draw grid
pDC->MoveTo(r.right/3,0);
pDC->LineTo(r.right/3,r.bottom);
pDC->MoveTo(2*r.right/3,0);
pDC->LineTo(2*r.right/3,r.bottom);
pDC->MoveTo(0,r.bottom/3);
pDC->LineTo(r.right,r.bottom/3);
pDC->MoveTo(0,2*r.bottom/3);
pDC->LineTo(r.right,2*r.bottom/3);
// draw pieces
for (int x=0;x<3;x++)
for (int y=0;y<3;y++)
{
CPoint pt(x,y);
CPoint pt1(x+1,y+1);
GridToMouse(pt);
GridToMouse(pt1);
switch (pDoc->GetBoardState(x,y))
{
case X:
pDC->SelectObject(&xp);
pDC->MoveTo(pt.x+OFFSET,pt.y+OFFSET);
pDC->LineTo(pt1.x-OFFSET,pt1.y-OFFSET);
pDC->MoveTo(pt1.x-OFFSET,pt.y+OFFSET);
pDC->LineTo(pt.x+OFFSET,pt1.y-OFFSET);
break;
case O:
pDC->SelectObject(&op);
pDC->SelectStockObject(HOLLOW_BRUSH);
pDC->Ellipse(pt.x+OFFSET,pt.y+OFFSET,
pt1.x-OFFSET,pt1.y-OFFSET);
break;
}
}
pDC->SelectObject(old);
}
////////////////////////////////
// CMfctttView printing
BOOL CMfctttView::OnPreparePrinting(CPrintInfo* pInfo)
{
pInfo->SetMaxPage(2);
return DoPreparePrinting(pInfo);
}
// This OnPrint allows you to use MM_TEXT in your View
// and the print out scales appropriately
void CMfctttView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
CDC *dc=GetDC();
CString s;
int x=dc->GetDeviceCaps(LOGPIXELSX);
int y=dc->GetDeviceCaps(LOGPIXELSY);
int x1=pDC->GetDeviceCaps(LOGPIXELSX);
int y1=pDC->GetDeviceCaps(LOGPIXELSY);
// print header when printing only
s.Format("Tic Tac Toe game: %s",
GetDocument()->GetTitle());
pDC->TextOut(0,0,s);
pDC->MoveTo(0,75);
pDC->LineTo(pInfo->m_rectDraw.Width(),75);
if (pInfo->m_nCurPage==2)
{
CMfctttDoc *doc=GetDocument();
s.Format("Games I Won=%d, Games I Lost=%d"
" Draw Games=%d",doc->wins,doc->loss,
doc->draw);
pDC->TextOut(0,100,s);
return;
}
// Alter mapping mode so that pixels scale correctly
pDC->SetMapMode(MM_ISOTROPIC);
pDC->SetWindowExt(x,y);
pDC->SetViewportExt(x1,y1);
// top margin of 100 units
pDC->SetViewportOrg(0,100);
CView::OnPrint(pDC, pInfo);
}
void CMfctttView::OnBeginPrinting(CDC* /*pDC*/,
CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMfctttView::OnEndPrinting(CDC* /*pDC*/,
CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
////////////////////////////////
// CMfctttView diagnostics
#ifdef _DEBUG
void CMfctttView::AssertValid() const
{
CView::AssertValid();
}
void CMfctttView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMfctttDoc* CMfctttView::GetDocument()
{
ASSERT(m_pDocument->IsKindOf(
RUNTIME_CLASS(CMfctttDoc)));
return (CMfctttDoc*)m_pDocument;
}
#endif //_DEBUG
////////////////////////////////
// CMfctttView message handlers
// Convert mouse coordinate to grid coordinate
void CMfctttView::MouseToGrid(CPoint &pt)
{
CRect r;
GetClientRect(&r);
pt.x/=r.right/3;
pt.y/=r.bottom/3;
}
// Convert grid coordinate to mouse coordinate
void CMfctttView::GridToMouse(CPoint &pt)
{
CRect r;
GetClientRect(&r);
pt.x*=r.right/3;
pt.y*=r.bottom/3;
}
void CMfctttView::OnLButtonDown(UINT nFlags, CPoint point)
{
CMfctttDoc *doc=GetDocument();
// Convert to grid position
MouseToGrid(point);
// if not empty, beep and forget it
if (doc->GetBoardState(point.x,point.y)!=EMPTY)
{
MessageBeep(MB_ICONEXCLAMATION);
return;
}
// Empty -- put an X or an O here depending on the turn #
// Note: this was here for debugging,
// now the computer always plays O, so the
// move is always an X
doc->SetBoardState(point.x,point.y,
(doc->turn&1)?O:X);
doc->turn++;
doc->UpdateAllViews(NULL);
doc->Play(); // Do our move
}
void CMfctttView::OnEditUndo()
{
CMfctttDoc *doc=GetDocument();
doc->undo(TRUE);
}
void CMfctttView::OnUpdateEditUndo(CCmdUI* pCmdUI)
{
CMfctttDoc *doc=GetDocument();
pCmdUI->Enable(doc->undo(FALSE));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -