📄 myfirstgameview.cpp
字号:
// MyFirstGameView.cpp : implementation of the CMyFirstGameView class
//
#include "stdafx.h"
#include "MyFirstGame.h"
#include "MyFirstGameDoc.h"
#include "MyFirstGameView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyFirstGameView
IMPLEMENT_DYNCREATE(CMyFirstGameView, CView)
BEGIN_MESSAGE_MAP(CMyFirstGameView, CView)
//{{AFX_MSG_MAP(CMyFirstGameView)
ON_COMMAND(ID_PRIMARY, OnPrimary)
ON_COMMAND(ID_SENIOR, OnSenior)
ON_COMMAND(ID_MIDDLE, OnMiddle)
ON_WM_LBUTTONDOWN()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMyFirstGameView construction/destruction
CMyFirstGameView::CMyFirstGameView()
{
m_IsSelectedOne =false;
int i;
for(i = 0; i<36; i++)
FGameBmp[i].LoadBitmap(IDB_BITMAP1+i);
}
CMyFirstGameView::~CMyFirstGameView()
{
}
BOOL CMyFirstGameView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyFirstGameView drawing
void CMyFirstGameView::OnDraw(CDC* pDC)
{
CDC MemDc;
CRect ClientRect;
GetClientRect(&ClientRect);
CBitmap MemBitmap;
MemBitmap.CreateCompatibleBitmap(pDC,ClientRect.Width(),ClientRect.Height());
MemDc.CreateCompatibleDC(pDC);
MemDc.SelectObject(&MemBitmap);
CMyFirstGameDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
/* DrawBackGround(pDC, IDB_BITMAPBACKGROUND);*/
DrawBmpBlock(&MemDc,&m_mygame);
DrawRectangle(&MemDc);
pDC->BitBlt(0,0,ClientRect.Width(),ClientRect.Height(),&MemDc,0,0,SRCCOPY);
}
/////////////////////////////////////////////////////////////////////////////
// CMyFirstGameView printing
BOOL CMyFirstGameView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyFirstGameView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyFirstGameView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyFirstGameView diagnostics
#ifdef _DEBUG
void CMyFirstGameView::AssertValid() const
{
CView::AssertValid();
}
void CMyFirstGameView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyFirstGameDoc* CMyFirstGameView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyFirstGameDoc)));
return (CMyFirstGameDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyFirstGameView message handlers
void CMyFirstGameView::DrawBackGround(CDC* pDC, UINT nIDBitmap)
{
CBitmap LBitmap;
if(LBitmap.LoadBitmap(nIDBitmap))
{
BITMAP LBitmapInfo;
LBitmap.GetBitmap(&LBitmapInfo);
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = dcMemory.SelectObject(&LBitmap);
CRect rect;
GetClientRect(&rect);
int nX = rect.left + (rect.Width() - LBitmapInfo.bmWidth) / 2;
int nY = rect.top + (rect.Height() - LBitmapInfo.bmHeight) / 2;
pDC->BitBlt(nX, nY, LBitmapInfo.bmWidth, LBitmapInfo.bmHeight, &dcMemory,
0, 0, SRCCOPY);
dcMemory.SelectObject(pOldBitmap);
LBitmap.DeleteObject() ;
}
else
{
TRACE("EXit: Can't Load Bitmap");
}
}
void CMyFirstGameView::OnPrimary()
{
m_mygame.SetClass(sSimple);
m_mygame. InitialData();
m_IsSelectedOne = 0;
Invalidate();
}
void CMyFirstGameView::OnSenior()
{
m_mygame.SetClass(sHard);
m_mygame. InitialData();
m_IsSelectedOne = 0;
Invalidate();
}
void CMyFirstGameView::OnMiddle()
{
m_mygame.SetClass(sMiddle);
m_mygame. InitialData();
m_IsSelectedOne = 0;
Invalidate();
}
void CMyFirstGameView::DrawBmpBlock(CDC *pDC, CMyGame *mygame)
{
/* CBitmap MemBitmap;
CRect ClientRect;
GetClientRect(&ClientRect);
MemBitmap.CreateCompatibleBitmap(pDC, ClientRect.Width(), ClientRect.Height());
MemDc.CreateCompatibleDC(pDC);
MemDc.SelectObject(&MemBitmap);*/
int i,j;
for(i= 1 ;i<mygame->m_Col + 1;i++)
{
for(j=1;j<mygame->m_row + 1;j++)
{
if (mygame->pData[i][j]!=0 )
{
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = dcMemory.SelectObject(&FGameBmp[mygame->pData[i][j]-1]);
int x, y ;
mygame->TranToXY(&x, &y, i, j) ;
pDC->BitBlt(x, y, mygame->m_dx, mygame->m_dy, &dcMemory,
0, 0, SRCCOPY);
/*dcMemory.SelectObject(pOldBitmap);*/
}
}
}
// pDC->BitBlt(0,0,ClientRect.Width(),ClientRect.Height(),&MemDc,0,0 ,SRCCOPY);
}
void CMyFirstGameView::OnLButtonDown(UINT nFlags, CPoint point)
{
int xpos, ypos, x, y, midx1, midy1, midx2, midy2;
CRect rect;
CClientDC dc(this);
m_mygame.TranXYToPosition(&xpos, &ypos, point.x, point.y);
m_mygame.TranToXY(&x, &y, xpos, ypos);
if (m_mygame.pData[xpos][ypos] ==0)
exit;
if (m_IsSelectedOne == 0)
{
m_IsSelectedOne = 1;
m_SelectedPoint.x = xpos;
m_SelectedPoint.y = ypos;
rect.left = x;
rect.top =y;
rect.right = x + m_mygame.m_dx;
rect.bottom = y + m_mygame.m_dy;
dc.Rectangle(&rect);
}
else if(m_IsSelectedOne == 1)
{
m_mygame.TranXYToPosition(&xpos, &ypos, point.x, point.y);
m_mygame.TranToXY(&x, &y, xpos, ypos);
if (!((m_SelectedPoint.x == xpos) && (m_SelectedPoint.y == ypos) )
&& (m_mygame.pData[m_SelectedPoint.x][m_SelectedPoint.y] ==m_mygame.pData[xpos][ypos]))
{
if(m_mygame.TestLine(m_SelectedPoint.x, m_SelectedPoint.y, xpos, ypos, midx1, midy1, midx2,midy2) )
{
rect.left = x;
rect.top =y;
rect.right = x + m_mygame.m_dx;
rect.bottom = y + m_mygame.m_dy;
dc.Rectangle(&rect);
m_mygame.TranToXY(&x, &y, xpos, ypos );
dc.MoveTo(x+m_mygame.m_dx /2, y+m_mygame.m_dy /2);
m_mygame.TranToXY(&x, &y, midx1, midy1);
dc.LineTo(x+m_mygame.m_dx /2, y+m_mygame.m_dy /2);
m_mygame.TranToXY(&x, &y, midx2, midy2);
dc.LineTo(x+m_mygame.m_dx /2, y+m_mygame.m_dy /2);
m_mygame.TranToXY(&x, &y, m_SelectedPoint.x, m_SelectedPoint.y);
dc.LineTo(x+m_mygame.m_dx /2, y+m_mygame.m_dy /2);
m_mygame.pData[m_SelectedPoint.x][m_SelectedPoint.y] =0;
m_mygame.pData[xpos][ypos]=0;
Sleep(100);
}
m_IsSelectedOne = 0;
}
else
m_IsSelectedOne= 0;
}
Invalidate();
CView::OnLButtonDown(nFlags, point);
}
void CMyFirstGameView::DrawRectangle(CDC* pDc)
{
int xpos, ypos, x, y;
CRect rect;
m_mygame.TranToXY(&x, &y, m_SelectedPoint.x, m_SelectedPoint.y);
if (m_IsSelectedOne)
{
rect.left = x;
rect.top =y;
rect.right = x + m_mygame.m_dx;
rect.bottom = y + m_mygame.m_dy;
pDc->MoveTo(x,y);
pDc->LineTo(x,rect.bottom);
pDc->LineTo(rect.right, rect.bottom);
pDc->LineTo(rect.right,y);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -