📄 cchessboardview.cpp
字号:
// CChessBoardView.cpp : implementation of the CCChessBoardView class
//
#include "stdafx.h"
#include "CChessBoard.h"
#include "DialogHelp.h"
#include "CChessBoardDoc.h"
#include "CChessBoardView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCChessBoardView
IMPLEMENT_DYNCREATE(CCChessBoardView, CView)
BEGIN_MESSAGE_MAP(CCChessBoardView, CView)
//{{AFX_MSG_MAP(CCChessBoardView)
ON_COMMAND(ID_VIEW_SIZE, OnViewSize)
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_DIALOG_HELP, OnDialogHelp)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CCChessBoardView construction/destruction
CCChessBoardView::CCChessBoardView()
{
// TODO: add construction code here
}
CCChessBoardView::~CCChessBoardView()
{
CCChessBoardDoc* pDoc = GetDocument();
pDoc->DeleteBoard(m_Size);
}
BOOL CCChessBoardView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCChessBoardView drawing
void CCChessBoardView::OnDraw(CDC* pDC)
{
CCChessBoardDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(m_DrawBoard==true){
FillBlock(pDC);
Drawboard(pDC);
}
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CCChessBoardView printing
BOOL CCChessBoardView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCChessBoardView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCChessBoardView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCChessBoardView diagnostics
#ifdef _DEBUG
void CCChessBoardView::AssertValid() const
{
CView::AssertValid();
}
void CCChessBoardView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCChessBoardDoc* CCChessBoardView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCChessBoardDoc)));
return (CCChessBoardDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCChessBoardView message handlers
void CCChessBoardView::OnViewSize()
{
// set m_size both in the doc
CCChessBoardDoc* pDoc = GetDocument();
pDoc->m_SizeDialog->DoModal();
if(pDoc->m_SizeDialog->CheckSize()==false) return;
pDoc->SetSize(pDoc->m_SizeDialog->GetSize());
//initlize the board
pDoc->DeleteBoard(m_Size); //first use the old m_size delete
//the old board
m_Size = pDoc->GetSize();
pDoc->InitBoard(m_Size);
Board = pDoc->Board;
for(int i = 0;i<m_Size;i++)
for(int j = 0;j<m_Size;j++)
Board[i][j] = 0;
this->Invalidate();
//draw the board
CClientDC dc(this);
Drawboard(&dc);
m_DrawBoard = true;
}
void CCChessBoardView::fn_ChessBoard(int tr, int tc, int dr, int dc, int size)
{
if (size == 1)
return;
int t = tile++; // L型骨牌数
int s = size / 2; // 分割棋盘
//覆盖左上角子棋盘
if (dr < tr + s && dc < tc + s) //特殊方格在此棋盘中
fn_ChessBoard(tr, tc, dr, dc, s);
else{//此棋盘中无特殊方格
Board[tr+s-1][tc+s-1]=t; //用t号L型骨牌覆盖右下角
fn_ChessBoard(tr,tc,tr+s-1,tc+s-1,s); //覆盖其余方格
}
//覆盖右上角子棋盘
if (dr < tr + s && dc >= tc + s) //特殊方格在此棋盘中
fn_ChessBoard(tr, tc + s, dr, dc, s);
else{//此棋盘中无特殊方格
Board[tr+s-1][tc+s]=t; //用t号骨牌覆盖左下角
fn_ChessBoard(tr,tc+s,tr+s-1,tc+s,s); //覆盖其余方格
}
//覆盖左下角子棋盘
if (dr >= tr + s && dc < tc + s) //特殊方格在此棋盘中
fn_ChessBoard(tr + s, tc, dr, dc, s);
else{//此棋盘中无特殊方格
Board[tr + s][tc+s-1]=t; //用t号骨牌覆盖右上角
fn_ChessBoard(tr + s, tc, tr+s,tc+s-1,s); //覆盖其余方格
}
//覆盖右下角子棋盘
if (dr >= tr + s && dc >= tc + s) //特殊方格在此棋盘中
fn_ChessBoard(tr + s, tc + s, dr, dc, s);
else{//此棋盘中无特殊方格
Board[tr + s][tc+s]=t; //用t号骨牌覆盖左上角
fn_ChessBoard(tr + s, tc + s, tr+s,tc+s,s); //覆盖其余方格
}
}
//fill the chess board
void CCChessBoardView::FillBlock(CDC *pDC)
{
int length = m_length;
int blocksize = m_Size;
int i = 0;
int j =0;
int AllLength = length * blocksize;
int red =255;
int green = 255;
int blue = 255;
RECT rect;
for (i = 0; i<blocksize;i++)
for(j = 0;j<blocksize;j++)
{
rect.top = yy+i*length;
rect.left =xx+j*length;
rect.bottom = rect.top+length;
rect.right = rect.left+length;
if(Board[i][j]==0) {
red = 255;
green =255;
blue =255;
}
else
{
red = Board[i][j]*52 ;
green =Board[i][j]*33;
blue = Board[i][j]*13 ;
//red = 0;
// green =0;
// blue = 0;
}
CBrush bru, * pbru;
pbru = &bru;
pbru->CreateSolidBrush(RGB(red,green,blue));
pDC->FillRect(&rect, pbru);
}
}
void CCChessBoardView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
Board = NULL;
m_length = 20;
m_Size = 0;
tile = 1;
xx = 200;
yy = 100;
m_SelectPoint.x = 0;
m_SelectPoint.y = 0;
dr = 0;
dc = 0;
m_DrawBoard = false; //decide when the WM_PAINT the windows weather can be
//draw
}
// Draw the chessboard
void CCChessBoardView::Drawboard(CDC *pDC)
{
int length = m_length;
int blocksize = m_Size;
int i = 0;
int AllLength = length * blocksize;
int x =xx;
int y =yy;
CPen pen;
pen.CreatePen(PS_SOLID,2,RGB(255,0,0));
CPen* oldPen = pDC->SelectObject(&pen);
for (i = 0; i <= blocksize; i ++ ){
pDC->MoveTo(x,yy + i*length);
pDC->LineTo(x+AllLength,y+i*length);
}
for (i = 0; i <= blocksize; i++){
pDC->MoveTo(x + i*length,y);
pDC->LineTo(x + i*length,y + AllLength);
}
pDC->SelectObject(oldPen);
}
//DEL void CCChessBoardView::OnViewDrawboard()
//DEL {
//DEL // TODO: Add your command handler code here
//DEL CClientDC dc(this);
//DEL Drawboard(&dc);
//DEL m_LBottonEnable = true;
//DEL m_DrawBoard = true;
//DEL
//DEL }
//DEL void CCChessBoardView::OnViewFillboard()
//DEL {
//DEL // TODO: Add your command handler code here
//DEL // if(m_LBottonChecked == true){
//DEL
//DEL //
//DEL // fn_ChessBoard(0, 0, dc, dr,m_Size);
//DEL // CClientDC DC(this);
//DEL // FillBlock(&DC);
//DEL // Drawboard(&DC);
//DEL // m_LBottonChecked = false;
//DEL // for(int i = 0;i<m_Size;i++)
//DEL // for(int j = 0;j<m_Size;j++)
//DEL // Board[i][j] = 0;
//DEL // //
//DEL // }
//DEL
//DEL }
void CCChessBoardView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_SelectPoint.x = point.x;
m_SelectPoint.y = point.y;
dr = (m_SelectPoint.x-xx )/m_length;
dc = (m_SelectPoint.y-yy )/m_length;
CView::OnLButtonDown(nFlags, point);
//before use the Board ,clear it
for(int i = 0;i<m_Size;i++)
for(int j = 0;j<m_Size;j++)
Board[i][j] = 0;
fn_ChessBoard(0, 0, dc, dr,m_Size);
CClientDC DC(this);
FillBlock(&DC);
Drawboard(&DC);
}
void CCChessBoardView::OnDialogHelp()
{
// TODO: Add your command handler code here
CDialogHelp help;
help.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -