// MxCubesView.cpp : implementation of the CMxCubesView class
// Download by http://www.srcfans.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;
评论0
最新资源