📄 hebaiview.cpp
字号:
// hebaiView.cpp : implementation of the CHebaiView class
//
#include "stdafx.h"
#include "hebai.h"
#include "hebaiDoc.h"
#include "hebaiView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHebaiView
IMPLEMENT_DYNCREATE(CHebaiView, CView)
BEGIN_MESSAGE_MAP(CHebaiView, CView)
//{{AFX_MSG_MAP(CHebaiView)
ON_WM_LBUTTONUP()
ON_COMMAND(ID_FILE_NEW, OnFileNew)
ON_COMMAND(IDM_COLORONE, OnColorone)
ON_COMMAND(IDM_COLORZERO, OnColorzero)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CHebaiView construction/destruction
CHebaiView::CHebaiView()
{
// TODO: add construction code here
m_colOne = RGB(255,0,0);
m_colZero = RGB(0,0,255);
m_iCount = 0;
}
CHebaiView::~CHebaiView()
{
}
BOOL CHebaiView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CHebaiView drawing
void CHebaiView::OnDraw(CDC* pDC)
{
CHebaiDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
DrawClient(pDC);
DrawCount(pDC);
}
/////////////////////////////////////////////////////////////////////////////
// CHebaiView printing
BOOL CHebaiView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CHebaiView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CHebaiView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CHebaiView diagnostics
#ifdef _DEBUG
void CHebaiView::AssertValid() const
{
CView::AssertValid();
}
void CHebaiView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CHebaiDoc* CHebaiView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHebaiDoc)));
return (CHebaiDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CHebaiView message handlers
//获取边的宽度
//宽度为屏幕宽度的十分之一
void CHebaiView::GetSideSize()
{
CRect rect ;
GetClientRect(&rect);
if( rect.Width() == 0)
{
this->m_uSideSize=0;
}
m_uSideSize =rect.Width()/10;
}
//获取左上角坐标
void CHebaiView::GetTopPoint()
{
CRect rect ;
GetClientRect(&rect);
if( rect.Width() == 0)
{
this->m_pointTop.x =0;
}
if( rect.Height() ==0 )
{
this->m_pointTop.y =0;
}
m_pointTop.x = rect.left + rect.Width()/4;
if( rect.Height() < 5*m_uSideSize)
{
m_pointTop.y =0;
}
else
{
m_pointTop.y = rect.top + (rect.Height()-5*m_uSideSize)/2;
}
}
//开始画格
void CHebaiView::DrawClient(CDC *pDC)
{
//////////////////////////////////////////////////
//变量定义
//////////////////////////////////////////////////
int i =0 ,
j=0 ;
int nValue ;
CRect rect;
CBrush brOne,
brZero,
*pbrOld;
//////////////////////////////////////////////////
//变量定义
//////////////////////////////////////////////////
//////////////////////////////////////////////////
//初始化
//////////////////////////////////////////////////
if ( ::IsBadReadPtr(pDC,sizeof(CDC)))
{
return ;
}
GetSideSize();
GetTopPoint();
brOne.CreateSolidBrush(m_colOne);
brZero.CreateSolidBrush(m_colZero);
pbrOld = pDC->SelectObject( &brOne );
//////////////////////////////////////////////////
//初始化
//////////////////////////////////////////////////
for ( i=0; i<XSIZE ; i++)
{
rect.top = m_pointTop.y + i*m_uSideSize;
rect.bottom = rect.top + m_uSideSize;
for( j=0; j<YSIZE ; j++ )
{
rect.left = m_pointTop.x + j*m_uSideSize;
rect.right = rect.left + m_uSideSize;
nValue = m_game.GetTempGameAry(i,j);
pDC->SelectObject( nValue?&brOne:&brZero );
pDC->Rectangle(&rect);
}
}
pDC->SelectObject(&pbrOld);
}
//发生鼠标单击时间检查单击位置
bool CHebaiView::GetPoint(int &x, int &y)
{
GetSideSize();
GetTopPoint();
if( x < m_pointTop.x
|| x > m_pointTop.x + XSIZE * m_uSideSize
|| y < m_pointTop.y
|| y > m_pointTop.y + YSIZE * m_uSideSize )
{
return false ;
}
x = (x-m_pointTop.x)/m_uSideSize;
y = (y-m_pointTop.y)/m_uSideSize;
return true;
}
void CHebaiView::OnLButtonUp(UINT nFlags, CPoint point)
{
int x,
y;
CString strTemp;
x=point.x;
y=point.y;
if ( GetPoint(x,y))
{
m_iCount ++;
m_game.SetTempGameAry(y,x,0);
Invalidate(false);
}
CView::OnLButtonUp(nFlags, point);
}
void CHebaiView::OnFileNew()
{
m_iCount =0;
m_game.InitGameAry();
Invalidate(false);
}
void CHebaiView::DrawCount(CDC *pDC)
{
CBrush brNew,*pbrOld;
if ( IsBadReadPtr(pDC,sizeof(CDC)))
{
return ;
}
CString strText ;
strText.Format("您已经走了%d步",m_iCount);
pDC->TextOut(10,20,strText);
pDC->TextOut(10,40,"要消除颜色");
brNew.CreateSolidBrush(this->m_colOne);
pbrOld=pDC->SelectObject(&brNew);
pDC->Rectangle(10,60,80,80);
pDC->SelectObject(pbrOld);
}
void CHebaiView::OnColorone()
{
CColorDialog colorDlg;
if(colorDlg.DoModal() == IDOK )
{
this->m_colOne = colorDlg.GetColor();
Invalidate(false);
}
}
void CHebaiView::OnColorzero()
{
CColorDialog colorDlg;
if(colorDlg.DoModal() == IDOK )
{
this->m_colZero = colorDlg.GetColor();
Invalidate(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -