www.gusucode.com > VC++开发的魔方游戏的源程序-源码程序 > VC++开发的魔方游戏的源程序-源码程序\code\MxCubesView.cpp

    // MxCubesView.cpp : implementation of the CMxCubesView class
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "MxCubes.h"

#include "MxCubesDoc.h"
#include "MxCubesView.h"

#include "WordBase.H"
#include "Cube.h"
#include "SetStepDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMxCubesView

IMPLEMENT_DYNCREATE(CMxCubesView, CView)

BEGIN_MESSAGE_MAP(CMxCubesView, CView)
	//{{AFX_MSG_MAP(CMxCubesView)
	ON_WM_ERASEBKGND()
	ON_COMMAND(ID_SYSTEM_SETBKBNDCOLOR, OnSystemSetbkbndcolor)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_FILE_SET, OnFileSet)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
	ON_COMMAND(ID_FILE_PRINT_SETUP, OnFilePrintSetup)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	ON_COMMAND(ID_FILE_RESTORE, OnFileRestore)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMxCubesView construction/destruction

CMxCubesView::CMxCubesView()
{
	// TODO: add construction code here
	BACKGROUND = RGB(0,0,0);
	m_nDownButton = -1;
	m_xBtnDefColor = RGB(192,192,192);
	m_bOK = TRUE;
	RANDSTEP = 10;
}

CMxCubesView::~CMxCubesView()
{
}

BOOL CMxCubesView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMxCubesView drawing

void CMxCubesView::OnDraw(CDC* pDC)
{
	CMxCubesDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CDC dc;
	CBitmap xBMP;
	CBitmap* pxSavedBMP = NULL;
	CRect xClientRect;
	GetClientRect(&xClientRect);
	if(pDC->IsPrinting()) return;
	if(!dc.CreateCompatibleDC(pDC)) return;
	if(!xBMP.CreateCompatibleBitmap(pDC, xClientRect.Width(), xClientRect.Height())) return;
	OnPrepareDC(&dc, NULL);
	pxSavedBMP = dc.SelectObject(&xBMP);

	// 下面开始用dc绘图
	dc.FillSolidRect(&xClientRect, BACKGROUND);
	DrawView(&dc);
//	m_xCube.Show(&dc);

	// 显示到屏幕
	pDC->BitBlt(0,0,xClientRect.Width(),xClientRect.Height(),&dc,0,0, SRCCOPY);
	dc.SelectObject(pxSavedBMP);
}

/////////////////////////////////////////////////////////////////////////////
// CMxCubesView printing

BOOL CMxCubesView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMxCubesView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMxCubesView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMxCubesView diagnostics

#ifdef _DEBUG
void CMxCubesView::AssertValid() const
{
	CView::AssertValid();
}

void CMxCubesView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMxCubesDoc* CMxCubesView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMxCubesDoc)));
	return (CMxCubesDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMxCubesView message handlers

BOOL CMxCubesView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	
	return TRUE; // CView::OnEraseBkgnd(pDC);
}

void CMxCubesView::OnSystemSetbkbndcolor() 
{
	// TODO: Add your command handler code here
	CColorDialog xCD;
	if(xCD.DoModal() == IDOK)
	{
		BACKGROUND = xCD.GetColor();
		Invalidate();
	}
}

void CMxCubesView::DrawView(CDC *pDC)
{
	CRect xCR;		// Client  Rect
	GetClientRect(&xCR);
	CRect xCtrlR;	// Control Rect
	xCtrlR = CRect(3*xCR.Width()/4,xCR.Height()/4,7*xCR.Width()/8, 3*xCR.Height()/4);
	pDC->FillRect(&xCtrlR, &CBrush(m_xBtnDefColor));
//	CRect xBR[4];	// Button  Rect
	int n;
	int W = xCtrlR.Width() ;
	int H = xCtrlR.Height();
	int L = xCtrlR.left;
	int T = xCtrlR.top ;
	for(n=0; n<4; n++)
	{
		m_xBR[n] = CRect(L+W/6,T+H/16+n*7*H/28,L+5*W/6,T+H/16+int((n+0.5)*7*H/28));
		pDC->DrawEdge(&m_xBR[n], EDGE_RAISED, BF_RECT);
		PutTitle(pDC, n, true);
	}

	// 画魔方的轮廓
	POINT V0;
	V0.x = xCR.Width()/4;
	V0.y = 10*xCR.Height()/21;
	int nSL;
	int nMin;
	if(xCR.Width()<xCR.Height())
		nMin = xCR.Width();
	else 
		nMin = xCR.Height();
	if(::GetSystemMetrics(SM_CXSCREEN)<1000)
		nSL = nMin/3;
	else
		nSL = nMin/4;
	m_xCube.ShowCube(pDC, V0, nSL);
	m_xCubeRect = CRect(V0.x-5,V0.y-int(0.8*nSL),V0.x+5+int(1.8*nSL),V0.y+nSL+5);
}

void CMxCubesView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int n;
	SetCapture();
	for(n=0; n<4; n++)
	{
		if(!m_xBR[n].PtInRect(point)) continue;
		m_nDownButton = n;
		CDC* pDC = GetDC();
		pDC->FillSolidRect(&m_xBR[m_nDownButton], m_xBtnDefColor);
		pDC->DrawEdge(&m_xBR[n], EDGE_SUNKEN, BF_RECT);
		PutTitle(pDC, m_nDownButton, false);
		ReleaseDC(pDC);
		break;
	}
	if(n<4) goto ENDTHIS;
	for(n=0; n<9; n++)
	{
		if(!m_xCube.m_xClickOgram[n].PtInOgram(point)) continue;
		m_xCube.RgnClicked(n);
		m_xCube.m_nStep = 20;
		InvalidateRect(&m_xCubeRect);
		JudgeOK();
		break;
	}

ENDTHIS:	
	CView::OnLButtonDown(nFlags, point);
}

void CMxCubesView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_nDownButton >= 0)
	{
		if(m_xBR[m_nDownButton].PtInRect(point))
		{
			m_xCube.Turnto(m_nDownButton); // 0~3: left right up down
			InvalidateRect(&m_xCubeRect);
		}
		CDC* pDC = GetDC();
		pDC->FillSolidRect(&m_xBR[m_nDownButton], m_xBtnDefColor);
		pDC->DrawEdge(&m_xBR[m_nDownButton], EDGE_RAISED, BF_RECT);
		PutTitle(pDC, m_nDownButton, true);
		m_nDownButton = -1;
		ReleaseDC(pDC);
	}
	ReleaseCapture();
	
	CView::OnLButtonUp(nFlags, point);
}

void CMxCubesView::PutTitle(CDC *pDC, int n, bool bRaised)
{
	static char  title[20];
	static short ccc[16]  ;
	static short turn[16] ;
	CWordBase::GetTurn(turn);
	switch(n)
	{
	case 0:
		strcpy(title, "向左");
		CWordBase::GetLeft(ccc);
		break;
	case 1:
		strcpy(title, "向右");
		CWordBase::GetRight(ccc);
		break;
	case 2:
		strcpy(title, "向上");
		CWordBase::GetUp(ccc);
		break;
	case 3:
		strcpy(title, "向下");
		CWordBase::GetDown(ccc);
		break;
	default:
		return;
	}
	const int wordlen = 16;
	CPoint c = m_xBR[n].CenterPoint();
	int x,y,k,cnt,off;
	cnt = 0;
	if(bRaised) off = 0;
	else off = 1;
	for(x=0; x<wordlen; x++)
	{
		k = 1;
		for(y=0; y<wordlen; y++)
		{
			if(turn[cnt] & k) pDC->SetPixelV(c.x-wordlen-wordlen/4+x+off,c.y-wordlen/2+y+off,RGB(0,0,255));
			k = k<<1;
		}
		cnt++;
	}
	cnt = 0;
	for(x=0; x<wordlen; x++)
	{
		k = 1;
		for(y=0; y<wordlen; y++)
		{
			if(ccc[cnt] & k) pDC->SetPixelV(c.x+wordlen/4+x+off,c.y-wordlen/2+y+off,RGB(255,0,0));
			k = k<<1;
		}
		cnt++;
	}
}

void CMxCubesView::OnFileNew() 
{
	// TODO: Add your command handler code here
//	InitMxCubes();
	m_xCube.Randomize(RANDSTEP);
	InvalidateRect(&m_xCubeRect);
	JudgeOK();
}

void CMxCubesView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int n;
	static HCURSOR xSave = GetCursor();
	for(n=0; n<9; n++)
	{
		if(!m_xCube.m_xClickOgram[n].PtInOgram(point)) continue;
		break;
	}
	switch(n/3)
	{
	case 0:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_FRONT));
		break;
	case 1:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_RIGHT));
		break;
	case 2:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_UP));
		break;
	default:
		::SetCursor(xSave);
		break;
	}
	
	CView::OnMouseMove(nFlags, point);
}

void CMxCubesView::JudgeOK()
{
	if(m_bOK != m_xCube.IsOK() && !m_bOK)
	{
		MessageBox("恭喜你呀,好聪明哟");
		m_bOK = TRUE;
	}
	m_bOK = m_xCube.IsOK();
}

void CMxCubesView::OnFileSet() 
{
	// TODO: Add your command handler code here
	CSetStepDlg xDlg;
	xDlg.m_nStep = RANDSTEP;
	if(xDlg.DoModal() == IDOK)
	{
		RANDSTEP = xDlg.m_nStep;
	}
}

void CMxCubesView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	
}

void CMxCubesView::OnFilePrint() 
{
	// TODO: Add your command handler code here
	
}

void CMxCubesView::OnFilePrintPreview() 
{
	// TODO: Add your command handler code here
	
}

void CMxCubesView::OnFilePrintSetup() 
{
	// TODO: Add your command handler code here
	
}

void CMxCubesView::OnFileSave() 
{
	// TODO: Add your command handler code here
	
}

void CMxCubesView::OnFileSaveAs() 
{
	// TODO: Add your command handler code here
	
}

void CMxCubesView::OnFileRestore() 
{
	// TODO: Add your command handler code here
	int  pnPath[100];
	int  nStepCnt   ;
	BOOL bFind = m_xCube.Restore(pnPath, nStepCnt);
	if(bFind == FALSE)
		MessageBox("对不起,在限定步数内无法恢复");
	else
		MessageBox("在限定步数内已经恢复");
	int _nStepCnt;
	int n;
	_nStepCnt = nStepCnt;
	nStepCnt = 0;
	for(n=0; n<_nStepCnt; n++)
	{
		int t;
		t = pnPath[n];
		if((t==pnPath[n+1]) || (t==pnPath[n+2]) || (t==pnPath[n+3]))
		{
			n+=4;
			continue;
		}
		pnPath[nStepCnt++] = pnPath[n];
	}
	CDC* pDC = GetDC();
	CString xWay;
	for(n=0; n<nStepCnt; n++)
	{
		xWay.Format("do %d", pnPath[n]);
		pDC->TextOut(10, 10+n*20, xWay);
	}
	ReleaseDC(pDC);
}